728x90

[org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

 

이직 후 회사에서 작업 을 진행하다 RestTemplate GET 방식에 body 를 넣는 코드를 작성 테스트중 특이한 점을 발견했다.

위와 같은 오류가 발생..

 

분명 postman 전송 테스트 시에는 잘 받았는데 실제 전송 을 호출하니 바디를 찾지 못해서 난감했다.

 

 

결론부터 말하자면

디폴트인 ClientHttpRequestFactory와 함께 RestTemplate를 사용하면,

JDK의 HttpURLConnection은 기본적인 HTTP client 이다.

HttpURLConnection은 GET요청과 함께 요청 바디를 보내지 않는다.

 

해결방안은 아래와 같다.

 

구글링으로 참조한 글

https://velog.io/@gkdud583/Spring-testrestTemplate-request-body-GET%EC%9A%94%EC%B2%AD

 

[Spring] testrestTemplate request body, GET요청

testrestTemplate를 이용해 코드를 작성하면서, 이상한 점을 발견했다.testrestTemplate.exchange()를 이용해, 요청 바디와 함께 GET요청을 보냈는데, 다음의 예외가 발생하면서 테스트에 실패했다. 그래서 tes

velog.io


아래는 stackoverflow 참조한 해결방안

https://stackoverflow.com/questions/62309635/resttemplate-get-with-body

 

RestTemplate get with body

How to make get with body using rest template? Based on question from: POST request via RestTemplate in JSON, I tried make GET with body via HttpEntity (just check if it is possible), but it failed

stackoverflow.com

 

해결방법으로 body factory 를 변경하는 방법도 확인했지만 적용을 하는게 좋을지는 판단이 되지 않아서,

기존에 Request Body로 받는 부분은 전송 시 queryString 과 엔드 포인트에서는 modelAttribute 로 받도록 처리를 변경하였다.

728x90

'코드 > dev' 카테고리의 다른 글

Junit test filtering  (2) 2023.11.17
비동기처리 시 Spring security 문제  (0) 2023.11.16
저장소에 올라간 브랜치 되돌리기  (2) 2023.11.15
spring AOP & AspectJ  (1) 2023.11.15
mariaDB Schedule  (2) 2023.11.14

+ Recent posts