美文网首页
SpringMVC集成fastjson

SpringMVC集成fastjson

作者: 张明学 | 来源:发表于2020-07-18 12:00 被阅读0次

请先参见:fastjson介绍与配置

在SpringMVC中定义FastJsonHttpMessageConverter

<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
                <!-- 避免IE出现下载JSON文件的情况 -->
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
        <property name="fastJsonConfig" ref="fastJsonConfig"></property>
    </bean>

也可以配置fastjson设置

<!-- fastJson的配置 -->
    <bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        <property name="serializerFeatures">
            <list>
                <value>DisableCircularReferenceDetect</value>
                <value>SortField</value>
            </list>
        </property>
        <property name="charset" value="UTF-8"></property>
        <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
    </bean>

也可以配置String的Converter

<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
    <!-- 避免出现乱码 -->
    <constructor-arg value="UTF-8" index="0" />
    <property name="supportedMediaTypes">
        <list>
            <value>text/plain;charset=UTF-8</value>
            <value>application/json;charset=UTF-8</value>
        </list>
    </property>
</bean>

配置使用

<mvc:annotation-driven>
    <mvc:message-converters>

        <ref bean="stringHttpMessageConverter" />
        <ref bean="fastJsonHttpMessageConverter"/>

    </mvc:message-converters>
</mvc:annotation-driven>

相关文章

网友评论

      本文标题:SpringMVC集成fastjson

      本文链接:https://www.haomeiwen.com/subject/debxkktx.html