반응형
AJAX 요청을 처리하는 도중, 다음과 같은 오류가 나왔다.
org.springframework.web.HttpMediaTypeNotSupportedException:
Content type 'application/json;charset=UTF-8' not supported
이 오류는 @ResponseBody 로 Map 객체를 반환하려고 할 때 발생했다.
이유는 Spring이 전달된 JSON 형식의 데이터를 올바르게 처리하지 못했기 때문이다.
즉, 클라이언트에서 전송한 JSON 데이터가 서버에서 예상한 형식과 일치하지 않아서 발생한 문제로, 이를 해결하기 위해서는 서버가 JSON 데이터를 올바르게 파싱할 수 있도록 설정을 해줘야 한다.
🐋 해결방안 01.
• contentType을 명시적으로 설정하기
01) 포스트맨 사용하는 경우 : Headers에 추가
02) AJAX 사용하는 경우 : 항목에 추가
$.ajax({
contentType: "application/json", // 추가하기
success: function (res) {
console.log(res);
},
error: function (error) {
console.log(error);
},
});
- 나는 이렇게 해줬는데도 계속 같은 오류가 발생하였다.
🐋 해결방안 02.
• pom.xml에 의존성 추가
<!-- jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<!-- jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
- 구글링 했을 땐 여기까지 나왔는데 그래도 오류가 발생했다.
🐋 해결방안 03.
• dispatcher-servlet.xml에 설정 추가
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
- 이렇게 세 가지 다 한 이후로 데이터를 정상적으로 처리할 수 있었다.
반응형
댓글