导航

springmvc 使用Jackson的配置

Posted on 2017-04-10 10:33  _eve  阅读(524)  评论(0编辑  收藏  举报
<!--start:使用Jackson 1.x的配置,需要导入的jar包:jackson-core-lpgl-xxx.jar、jackson-mapper-lgpl-xxx.jar -->
<bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
    <property name="messageConverters">
        <list>
            <!-- 设置返回字符串编码,避免使用@ResponseBody后返回乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name = "supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!-- json转换器 -->
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

        </list>
    </property>
</bean>
<!--end:使用Jackson 1.x的配置,需要导入的jar包:jackson-core-lpgl-xxx.jar、jackson-mapper-lgpl-xxx.jar -->
复制代码

#####################################################################################################################

复制代码
<!--start:使用Jackson 2.x的配置,需要导入的jar包:jackson-core-xxx.jar、jackson-annotations-xxx.jar、jackson-databind-xxx.jar-->
<!--通过处理器映射DefaultAnnotationHandlerMapping来开启支持@Controller注解-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!--通过处理器适配器AnnotationMethodHandlerAdapter来开启支持@RequestMapping注解-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <!-- 设置返回字符串编码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name = "supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!-- json转换器 -->
            <bean 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>
        </list>
    </property>
</bean>
<!--end:使用Jackson 2.x的配置,需要导入的jar包:jackson-core-xxx.jar、jackson-annotations-xxx.jar、jackson-databind-xxx.jar-->