【新】使用fastjson作为SpringMVC的HttpMessageConverter

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012190514/article/details/83895788

最近需要将SpringMVC默认的HttpMessageConverter由Jackson转为fastjson,但是网上搜索的fastjson版本太老了,springmvc.xml的配置文件信息需要更新,我用的fastjson版本是1.2.47

网上抄的老的配置信息:

<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>
                    <!-- 默认的意思就是不配置这个属性,配置了就不是默认了 -->
                       <!-- 是否输出值为null的字段 ,默认是false-->
                         
                        <value>WriteMapNullValue</value>
                         
                        <value>WriteNullNumberAsZero</value>
                        <value>WriteNullListAsEmpty</value>
                        <value>WriteNullStringAsEmpty</value>
                        <value>WriteNullBooleanAsFalse</value>
                        <value>WriteDateUseDateFormat</value>
                     
                    </list>
                </property>
            </bean>
                
     </mvc:message-converters>
</mvc:annotation-driven>

但是我们拷贝过去后发现,已经被弃用

于是我打开

FastJsonHttpMessageConverter

这个类

所以,我们需要这么配置:

<!-- 注入fastjson配置类 -->
	<bean name="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
		<property name="serializerFeatures">
			<list>
				<value>WriteMapNullValue</value>
				<value>WriteMapNullValue</value>

				<value>WriteNullNumberAsZero</value>
				<value>WriteNullListAsEmpty</value>
				<value>WriteNullStringAsEmpty</value>
				<value>WriteNullBooleanAsFalse</value>
				<value>WriteDateUseDateFormat</value>
			</list>
		</property>
	</bean>
	<mvc:annotation-driven >
		<mvc:message-converters>

			<!-- 这里配置alibaba 的fastjson -->
			<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8</value>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>

				<!-- 配置 -->
				<property name="fastJsonConfig" ref="fastJsonConfig">

				</property>
				


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

这么设置就完事了

猜你喜欢

转载自blog.csdn.net/u012190514/article/details/83895788