스프링/스프링부트
[Spring Boot] 오류 해결: (org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing)
책 읽는 개발자_테드
2021. 4. 28. 20:08
반응형
문제 상황
스프링 부트에서 다음과 같은 컨트롤러를 생성하고,
@RestController
public class UserController {
@GetMapping("users")
public ResponseEntity<?> select(@RequestBody User user) {
String introduction = "안녕하세요. 저의 이름은 " + user.getName()+ "입니다."
+ " 저의 취미는 " + user.getHobby() + "입니다.";
return ResponseEntity.ok(introduction);
}
}
다음과 같이 HTTP GET 요청을 보냈다. 하지만 돌아온 것은 400 Bad Request 에러였다.
그리고 콘솔창에는 다음과 같은 에러가 출력되었다.
org.springframework.http.converter.HttpMessageNotReadableException:
분명히 @RequestBody에 해당하는 메세지(name, hobby)를 함께 전송했지만, 컨트롤러는 이를 인식하지 못했다.
원인
HTTP GET 요청의 body 형식을 Text로 보냈기 때문이다.
해결
HTTP GET 요청의 body 형식을 JSON으로 변경하여 해결할 수 있었다.
다시 요청을 보내니 Controller가 정상적으로 응답을 보낸다.
반응형