springmvc与fastjson整合

本文主要是进行springmvc与fastjson整合

1.在pom.xml配置fastjson:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson-version}</version>
</dependency>

 

 2.web.xml配置:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>


    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/index.jsp</location>
    </error-page>

 3.springMVC-servlet.xml配置:

<context:component-scan base-package="com.ezubo.global.controller"/>

                <mvc:default-servlet-handler/>
                <mvc:annotation-driven >
                     <mvc:message-converters register-defaults="true">
<!--                            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>-->
<!--
                            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
-->
                            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
                                <!-- 转换时设置特性-->
                                <property name="features">
                                    <array>
                                        <!-- 避免默认的循环引用替换-->
                                        <value>DisableCircularReferenceDetect</value>
                                        <value>WriteNullStringAsEmpty</value>
                                        <value>WriteNullNumberAsZero</value>
                                        <value>WriteMapNullValue</value>
                                <!--    <ref bean="DisableCircularReferenceDetect"/>
                                        <ref bean="WriteMapNullValue"/>
                                        <ref bean="WriteNullStringAsEmpty"/>
                                        <ref bean="WriteNullNumberAsZero"/>-->
                                    </array>
                                </property>
                            </bean>
                     </mvc:message-converters>
                </mvc:annotation-driven>

                <mvc:resources mapping="/css/**" location="/css/"/>
                <mvc:resources mapping="/*.html" location="/"/>

                <!-- enum枚举值的引用方法 -->
                <bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
                    <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"></property>
                </bean>
                <bean id="WriteNullStringAsEmpty" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
                    <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.WriteNullStringAsEmpty"></property>
                </bean>
                <bean id="WriteNullNumberAsZero" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
                    <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.WriteNullNumberAsZero"></property>
                </bean>
                <bean id="WriteMapNullValue" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
                    <property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue"></property>
                </bean>

                <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                     <property name="prefix" value="/WEB-INF/jsp/"/>
                     <property name="suffix" value=".jsp"/>
                </bean>
</beans>

        <!-- fastjson-1.1.41与SpringMVC整合 -->
        <!--
        1)若按照jackson和SpringMVC的整合方式,应按照下面的写法,但测试发现这样会报告"HTTP Status 406"
          The resource identified by this request is only capable of generating responses
          with characteristics not acceptable according to the request "accept" headers.
        2)测试通过的整合方式为上面那样在mvc:annotation-driven里面进行注册
        3)supportedMediaTypes增加[text/html;charset=UTF-8]值,是为了兼容IE6
          否则[application/json]值在IE6中会导致弹出对话框询问是否保存文件,而firefox等高级浏览器会正常打印json字符串
        4)若像下面这样给supportedMediaTypes属性赋两个值[text/html;charset=UTF-8]和[application/json],则[application/json]是无效的
          因为此时应答给浏览器(或者说请求方)的Content-Type头信息都是[text/html;charset=UTF-8],所以给它一个值就行了
          如果给supportedMediaTypes的值为[application/json],则应答给浏览器的Content-Type头信息就是[application/json;charset=UTF-8]
        5)关于features属性,不是serializerFeature,而是features,详见FastJsonHttpMessageConverter.java
          它是用来控制json序列化输出时的一些额外属性,比如说该字段是否输出、输出时key使用单引号还是双引号、key不使用任何引号等等
          QuoteFieldNames===========输出key时是否使用双引号,默认为true
          WriteMapNullValue=========是否输出值为null的字段,默认为false
          WriteNullNumberAsZero=====数值字段如果为null,输出为0,而非null
          WriteNullListAsEmpty======List字段如果为null,输出为[],而非null
          WriteNullStringAsEmpty====字符类型字段如果为null,输出为"",而非null
          WriteNullBooleanAsFalse==Boolean字段如果为null,输出为false,而非null
        6)通常在网上搜到的fastjson和springMVC整合的例子中都像下面注释的代码那样给了两个属性WriteMapNullValue和QuoteFieldNames
          这就表示为json解析器设置QuoteFieldNames和WriteMapNullValue的值为true,即输出时key使用双引号,同时也输出值为null的字段
        7)输出时某字段为String类型,且值为null,此时若需要其输出,且输出值为空字符串,则需同时赋值WriteMapNullValue和WriteNullStringAsEmpty
          经测试,若只赋值WriteNullStringAsEmpty,则不会输出该字段..加上WriteMapNullValue属性后,便输出了,且输出值不是null,而是预期的空字符串
         -->
        <!--
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
          <property name="messageConverters">
            <list>
              <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                  <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json</value>
                  </list>
                </property>
                <property name="serializerFeature">
                  <array>
                    <value>QuoteFieldNames</value>
                    <value>WriteMapNullValue</value>
                  </array>
                </property>
              </bean>
            </list>
          </property>
        </bean>
         -->
        <!--
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
          <property name="messageConverters">
            <list>
              <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
              </bean>
            </list>
          </property>
        </bean>
         -->

 4.applicationContext.xml配置

<context:annotation-config/>

 

相关推荐