제목

Spring 406 not acceptable


상세

@RequestBody @ResponseBody 를 사용하고자 할 때 발생하는 문제점 해결방법


해결방법

@RequestBody / @ ReponseBody 어노테이션은 보통 View Resolver를 사용할 수 없고, 알맞은 HttpMessageConverter를 사용해야 한다.

위 어노테이션을 사용하기 위해서는, AnnotationMethodHandlerAdapter에 컨버터를 설정해야 하며,

참조문서에 잘 나와 있다.( 보통은 MappingJacksonHttpMessageConverter 를 사용하면 된다. )


ex) servlet-context.xml 설정 예시

<!-- 

title : 어노테이션 메소드 처리

added : 2013.03.06

maven : 

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-core-asl</artifactId>

<version>1.9.12</version>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.12</version>

</dependency>

-->

<beans:bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<beans:property name="messageConverters">

<beans:list>

<beans:ref bean="jsonConverter" />

<!-- <ref bean="marshallingConverter" /> <ref bean="atomConverter" /> -->

</beans:list>

</beans:property>

</beans:bean>


<beans:bean id="jsonConverter"

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<beans:property name="supportedMediaTypes" value="application/json" />

</beans:bean>


비고

참조 링크 

#1. http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable

#2. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestbody

#3. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-message-conversion

#4. http://loianegroner.com/2010/09/how-to-serialize-java-util-date-with-jackson-json-processor-spring-3-0




+ Recent posts