Spring MVC integrates fastjson simple configuration

Here is Official documents Introduction:

If you are using XML to configure Spring MVC, just add the following configuration to the XML configuration file of Spring MVC.

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>      
    </mvc:message-converters>
</mvc:annotation-driven>

Generally, the default configuration can meet most of the usage scenarios. If you want to customize it, you can add FastJsonConfig Bean.

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="fastJsonConfig" ref="fastJsonConfig"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
    <!--   Custom configuration...   -->
</bean>

Note: if you use Fastjson version less than 1.2.36 (the latest version is highly recommended), you need to use fastjsonhttpmessageconverter 4 when integrating with Spring MVC 4.X version.

Add relevant configurations according to your own needs:

<!-- Use Fastjson Provided FastJsonHttpMessageConverter To replace SpringMVC default HttpMessageConverter -->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="fastJsonConfig" ref="fastJsonConfig"/>
        </bean>
     </mvc:message-converters>
</mvc:annotation-driven>
<!-- custom Fastjson To configure -->
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
     <property name="features">
          <list>
              <!-- Prohibit fastjson Circular reference detection -->
              <value>DisableCircularReferenceDetect</value>
          </list>
     </property>
     <property name="serializerFeatures">
         <list>
             <!-- The output value is null Field of -->
             <value>WriteMapNullValue</value>
             <!-- Use fastjson Default time format -->
             <value>WriteDateUseDateFormat</value>
         </list>
     </property>
</bean>

In this way, the fastjson github address:

Go to

Tags: Spring xml less github

Posted on Mon, 04 May 2020 10:53:46 -0700 by valen53