pooney
Published 2020. 1. 15. 23:03
HttpMediaTypeNotSupportedException Spring

ajax를 사용시 type을 post를 이용할 때  controller에서 @requestbody를 사용해서 받을 때 아래와 같은 에러가 발생한다.  application / x-www-form-urlencoded를 사용할 때 Spring이 그것을 RequestBody로 이해하지 못한다는 것이다다. 따라서 이것을 사용하려면 @RequestBody 주석을 제거해야합니다 .

 

<java />
HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

 

클라이언트 

<java />
$.ajax({ url: "/scheduler/user/usercheck", type: "post", data: usercheckvalue, dataType : "text", success: function(data,req){ console.log(data); console.log(req); }, error: function(){ alert("simpleWithObject err"); } });

 

서버 ( @RequestBody UserDTO user  --> (어노테이션제거) UserDTO user )

<java />
@RequestMapping(value="/usercheck", method=RequestMethod.POST) @ResponseBody public String usercheck(UserDTO user){ return "test"; }

 

 

 

 

 

 

 contentType을 지정하는 방법 

JSON.stringify()을 사용하면서 contentType을 "application/json" 으로 지정하고 @RequestBody를 사용한다.  contentType을 "application/json"  으로 지정하지 않으면 RequestBody를 인식 할 수 없다. 

 

 

클라이언트 

<java />
$.ajax({ url: "/scheduler/user/usercheck", type: "post", data: JSON.stringify(usercheckvalue), dataType : "text", contentType: "application/json", success: function(data,req){ console.log(data); console.log(req); }, error: function(){ alert("simpleWithObject err"); } });

 

서버

<java />
@RequestMapping(value="/usercheck", method=RequestMethod.POST) @ResponseBody public String usercheck(@RequestBody UserDTO user){ return "test"; }

 

 

 

 

 

##참고 

 

https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for

 

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap

Based on the answer for problem with x-www-form-urlencoded with Spring @Controller I have written the below @Controller method @RequestMapping(value = "/{email}/authenticate", method = RequestMe...

stackoverflow.com

 

 

https://zzznara2.tistory.com/760

 

[Java] ajax로 java(Spring) 연동시 다음과 같은 에러 발생 해결방법 - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 이런 오류가 나네요. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37..

zzznara2.tistory.com

 

 

https://bigfat.tistory.com/103

 

[Spring] @RequestMapping의 produces 속성

@RequestMapping의 produces 속성을 이용해 Response의 Content-Type을 제어할 수 있다 한국관광공사가 제공하는 TourAPI를 사용하면서 지역코드(area_code)로 지역명(area_name)을 조회하는 간단한 요청에서 enco..

bigfat.tistory.com

 

'Spring' 카테고리의 다른 글

Spring 외부 설정 프로퍼티  (0) 2020.04.06
Spring 트랜잭션  (0) 2020.02.22
Spring 파일업로드  (0) 2020.02.20
Spring 버전 업 시 에러  (0) 2020.01.13
Spring mail 보내기  (0) 2020.01.03
profile

pooney

@pooney

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!