在 springmvc 中, 如何配置 fastjson (阿里开源的) 和 Jackson( Spring 内置的)

本文介绍如何在Spring MVC中使用Jackson和不同版本的FastJson来处理JSON数据,包括配置依赖及XML配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于使用的是maven工程,直接引入依赖即可
一、Jackson
引入

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.7.1</version>
		</dependency>

springmvc-servlet.xml中配置

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">      
        <property name="messageConverters">      
            <list>  
		<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">    
			<property name="supportedMediaTypes">    
			   <list>    
			      <value>application/json;charset=UTF-8</value>    
			      <value>application/xml;charset=UTF-8</value>   
			      <value>text/html;charset=UTF-8</value>
			  </list>    
			 </property>  
			</bean>    
            </list>      
        </property>      
    </bean> 

二、FastJson(spring4.2x版本以下)
引入

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>

springmvc-servlet.xml中配置


	<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>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                        <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

三、FastJson(spring4.2x版本以上)
引入

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.17</version>
		</dependency>

springmvc-servlet.xml中配置


	<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <ref bean="stringHttpMessageConverter" />  
            <ref bean="fastJsonHttpMessageConverter" />  
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <bean id="stringHttpMessageConverter"  
        class="org.springframework.http.converter.StringHttpMessageConverter">  
        <constructor-arg value="UTF-8" index="0"></constructor-arg>
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/plain;charset=UTF-8</value>  
            </list>  
        </property>  
    </bean>
    
    <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value> 
            </list>
        </property>
        <property name="fastJsonConfig">
        	<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
        		<property name="features">
	        		<list>
		                <value>AllowArbitraryCommas</value>
		                <value>AllowUnQuotedFieldNames</value>
		                <value>DisableCircularReferenceDetect</value>
		            </list>
        		</property>
        		<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
        	</bean>
        </property>
    </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值