The way of json conversion in springMVC

Record the problems encountered during the construction of the SSM framework:


In the springMVC framework, the @ResponseBody annotation is used to format the return value as json:

  1. Use the MappingJackson2HttpMessageConverter provided by the springMVC framework
  2. Use the FastJsonHttpMessageConverter provided by the fastjson framework of goole

spring-mvc .xml configuration method:

  1. MappingJackson2HttpMessageConverter method:

<bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="defaultCharset" value="UTF-8"/>
        <property name="supportedMediaTypes">  
            <list>  
                <value>application/json;charset=UTF-8</value>
            </list>  
        </property>  
    </bean>   
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>  
        </property>  
    </bean>


  2, FastJsonHttpMessageConverter method:

    <!-- Return json format data-->
    <bean id="jsonConverter"
          class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
        <property name="features">
            <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
                <value>DisableCircularReferenceDetect</value>
            </array>
        </property>
    </bean>
    <!--fastjson circular reference problem-->
    <bean id="DisableCircularReferenceDetect"
          class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField"
                  value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"></property>
    </bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!--<ref bean="mappingJacksonHttpMessageConverter"/>-->
                <ref bean="jsonConverter"/>          
            </list>
        </property>
    </bean>


Summary:

The first way :
  If the springMVC framework version is 4.1 and above, you need to import the jackson2.X jar package:
jackson-annotations-2.4.4.jar, jackson-core-2.4.4.jar, jackson-databind-2.4 .4.jar, if it is a version before 4.1, you can use jackson-core-asl-1.8.5.jar and jackson-mapper-asl-1.8.5.jar, the HttpMessageConverter for erq is MappingJacksonHttpMessageConverter.

The second way :
  To take into account the fastjson circular reference problem, the dependent jar package only needs Ali's fastjson to be ok, and fastjson is said to be much faster than jackson in terms of performance.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326785260&siteId=291194637