728x90

spring 3.2 이상에서 사용할 수 있는 messageConverter 방법

오늘 json 데이터를 가져오는 방법을 코드로 짜보면서 도움이 될 거 같아 정리했다.

servlet.xml 에 보면 "컨트롤러에서 넘어온 데이터(JSON 같은)를 messageConverter로 사용할 수 있는 방법 1" 과

"컨트롤러에서 넘어온 데이터(JSON 같은)를 messageConverter로 사용할 수 있는 방법 2" 가 있다.

원하는 방법으로 사용하면 된다.


아래는 Ref는 AnnotationMethodHandlerAdapter가 deprecated 된 이유를 알 수 있는 좋은 블로그가 있어서 참조했다.

Description Ref :)

http://javaiyagi.tistory.com/357

Spring 3.0 의 DefaultAnnotationHandlerMapping 과 AnnotationMethodHandlerAdapter가  Spring 3.1 에선 @Deprecated 되고, 

대신 RequestMappingHandlerMapping과 RequestMappingHandlerAdapter 로 바뀌었다.

이유인즉슨, Controller 의 요청이 메소드 단위로 세분화 되면서 , 

DefaultAnnotationHandlerMapping 이 AnnotationMethodHandlerAdapter로 handler 를 전달해줄 때, 문제가 생겼기 때문이다 .


pom.xml

<!-- jackson Json -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.7.3</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-core</artifactId>

<version>2.7.3</version>

</dependency>


<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-annotations</artifactId>

<version>2.7.3</version>

</dependency>

<dependency>

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

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

    <version>1.9.13</version>

</dependency>


<dependency>

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

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

    <version>1.9.13</version>

</dependency>


servlet-context.xml

<!-- 컨트롤러에서 넘어온 데이터(JSON 같은)를 messageConverter로 사용할 수 있는 방법 2 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

<property name="messageConverters">

    <util:list list-class="java.util.ArrayList">

<ref bean="mappingJackson2HttpMessageConverter"/>

    </util:list>

</property>

</bean>

<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

<property name="supportedMediaTypes">

    <list>

<value>text/html;charset=UTF-8</value>

<value>application/json;charset=UTF-8</value>

    </list>

</property>

</bean>

728x90

+ Recent posts