야후우
10
2020-05-17 23:46:45
1
89

Required request body is missing 오류 해결 도움 바랍니다


2020-05-17 23:22:15.080  INFO 900 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'

2020-05-17 23:22:15.081  INFO 900 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'

2020-05-17 23:22:15.092  INFO 900 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 11 ms

2020-05-17 23:22:15.363  WARN 900 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.study.model.network.Header<com.example.study.model.network.response.UserApiResponse> com.example.study.controller.api.UserApiController.create(com.example.study.model.network.Header<com.example.study.model.network.request.UserApiRequest>)]

2020-05-17 23:22:34.020  WARN 900 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.study.model.network.Header<com.example.study.model.network.response.UserApiResponse> com.example.study.controller.api.UserApiController.create(com.example.study.model.network.Header<com.example.study.model.network.request.UserApiRequest>)]

2020-05-17 23:23:57.211  WARN 900 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.study.model.network.Header<com.example.study.model.network.response.UserApiResponse> com.example.study.controller.api.UserApiController.create(com.example.study.model.network.Header<com.example.study.model.network.request.UserApiRequest>)]


이게 오류 부분 알림 부분입니다.


@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApiController implements CrudInterface<UserApiRequest,UserApiResponse> {

@Autowired
private UserApiLogicService userApiLogicService;



@Override
@PostMapping("") // /api/user
public Header<UserApiResponse> create(@RequestBody Header<UserApiRequest> userApiRequest) {
log.info("{}",userApiRequest);
return userApiLogicService.create(userApiRequest); // Service Controller를 연결 시켜 줌.

}
아래 post 부분이 restlet으로 localhost:8080/api/user를 테스트 하면 아무것도 안나오고 위의 오류가 뜹니다.
request가 잘 안들어왔다는 말인거 같아서 Header부분도 같이 올립니다. '


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data//게터 세터 자동으로 만들어준다.
@NoArgsConstructor//기본 생성자
@AllArgsConstructor//모든 매개변수를 가지고 있는 생성자
@Builder//생성자 쉽게 만들어주는 기능
public class Header<T> {

//api 통신 시간
//@JsonProperty("transaction_time") snake타입으로 표현
private LocalDateTime transactionTime;

//api 응답 코드
private String resultCode;

//api 부가 설명
private String description;

private T data;

// OK
public static <T> Header<T> OK(){
return (Header<T>)Header.builder()
.transactionTime(LocalDateTime.now())
.resultCode("OK")
.description("OK")
.build();
}

// DATA OK
public static <T> Header<T> OK(T data){
return (Header<T>)Header.builder()
.transactionTime(LocalDateTime.now())
.resultCode("OK")
.description("OK")
.data(data)
.build();
}

// ERROR
public static <T> Header<T> ERROR(String description){
return (Header<T>)Header.builder()
.transactionTime(LocalDateTime.now())
.resultCode("ERROR")
.description(description)
.build();
}
}

@Service
public class UserApiLogicService implements CrudInterface<UserApiRequest, UserApiResponse> {

@Autowired
private UserRepository userRepository;

// 1. request data
// 2. user 생성
// 3. 생성된 데이터 -> UserApiResponse return
@Override
public Header<UserApiResponse> create(Header<UserApiRequest> request) {

// 1. request data
UserApiRequest userApiRequest = request.getData();

// 2. User 생성
User user = User.builder()
.account(userApiRequest.getAccount())
.password(userApiRequest.getPassword())
.status("REGISTERED")
.phoneNumber(userApiRequest.getPhoneNumber())
.email(userApiRequest.getEmail())
.registeredAt(LocalDateTime.now())
.build();
User newUser = userRepository.save(user);

// 3. 생성된 데이터 -> userApiResponse return


return response(newUser);
}
private Header<UserApiResponse> response(User user){
// user -> userResponse

UserApiResponse userApiResponse = UserApiResponse.builder()
.id(user.getId())
.account(user.getAccount())
.password(user.getPassword()) // todo 암호화, 길이
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.status(user.getStatus())
.registeredAt(user.getRegisteredAt())
.unregisteredAt(user.getUnregisteredAt())
.build();

// Header + data return

return Header.OK(userApiResponse);
}
0
0
  • 답변 1

  • timeclip
    363
    2020-05-18 00:12:41
    데이터 보낼 때 헤더 content type 을 application/json 으로 보내보세요~
    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.