Fastjson、Jackson与SpringMVC整合的MessageConverter配置

1.Jackson

maven依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.7.1</version>
</dependency>

springmvc-servlet.xml中配置

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

2.FastJson

由于FastJson针对Spring4.2以后进行特殊优化,具体如图

所以FastJson可以分为Spring4.2及以下配置和Spring4.2以上的不同配置

2.1Spring4.2及以下配置

maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.7</version>
</dependency>

springmvc-servlet.xml中配置

<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                        <value>application/xml;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
</mvc:annotation-driven>

2.2Spring4.2以上配置

maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.7</version>
</dependency>

springmvc-servlet.xml中配置

<mvc:annotation-driven validator="validator">
        <mvc:message-converters register-defaults="true">
            <ref bean="stringHttpMessageConverter"/>
            <ref bean="fastJsonHttpMessageConverter"/>
        </mvc:message-converters>
</mvc:annotation-driven>
<!--FastJson(spring4.2x版本以上)-->
<bean id="stringHttpMessageConverter"
          class="org.springframework.http.converter.StringHttpMessageConverter">
        <constructor-arg value="UTF-8" index="0"></constructor-arg>
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
</bean>

<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
        <property name="fastJsonConfig">
            <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                <property name="features">
                    <list>
                        <value>AllowArbitraryCommas</value>
                        <value>AllowUnQuotedFieldNames</value>
                        <value>DisableCircularReferenceDetect</value>
                    </list>
                </property>
                <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
            </bean>
        </property>
</bean>

相关文章

SpringMVC1.MVC架构MVC是模型(Model)、视图(View)、控制...
SpringMVC学习笔记1.SpringMVC应用1.1SpringMVC简介​Spring...
11.1数据回显基本用法数据回显就是当用户数据提交失败时,自...
一、SpringMVC简介1、SpringMVC中重要组件DispatcherServlet...
1.它们主要负责的模块Spring主要应用于业务逻辑层。SpringMV...
3.注解开发Springmvc1.使用注解开发要注意开启注解支持,2.注...