본문 바로가기
spring | spring boot/오류 해결

[spring boot] js로 body넘겨줄때 controller에서 null값으로 받는 현상(swagger의 @RequestBody를 import 함)

by socialcomputer 2022. 3. 14.
반응형

spring boot로 백엔드, 뷰 부분을 thymleleaf를 사용해서 api로 통신하는 게시판을 만드는 중이었다. 

자바스크립트로 fetch()를 통해 post 방식으로 요청하고 있었다. 

그런데 swagger와 junit test를 수행 했을 때는 완벽했던 것이 전혀 말을 듣지 않았다. 

 

먼저 그 이유는 

@RequestBody 를 spring이 아닌 swagger에서 import 했기 때문이다. 
엄청난 검색 끝에,, stack over flow에서 찾아냈다.. 설마 import 가 잘못 됐을 줄이야...

 

시도했던 방법들은

1. js의 stringify로 변환되기 전의 params에 ""붙이기 -> 이 방식은 전혀 아님..

2. dto 의 필드에 @JsonProperty 붙이기 -> java와 js에서의 다른 형식인 이름을 매칭해줌.

3. jackon data bind 의존성 추가하기

 

도움이 된 곳

https://stackoverflow.com/questions/38935912/requestbody-is-getting-null-values

 

@RequestBody is getting null values

I have created a simple REST service (POST). But when i call this service from postman @RequestBody is not receiving any values. import org.springframework.http.MediaType; import org.springframewo...

stackoverflow.com

 

반응형

댓글