不管是性能、易用性、特性支持,fastjson都要遠好於默認的jackson,因此若是應用程序常用ajax進行數據交互,建議用fastjson做爲默認解析器,只須要簡單配置:html
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <!--設置fastjson特性--> <property name="features"> <array> <!--設置null值也要輸出,fastjson默認是關閉的--> <value>WriteMapNullValue</value> <!--設置使用文本方式輸出日期,fastjson默認是long--> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
而後引入fastjson的包就行了。
mysql
附Spring MVC4示例配置文件:web
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--包掃描--> <context:component-scan base-package="com.rs" /> <!--數據鏈接池,此處使用c3p0--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://x.x.x.x:3306/test" /> <property name="user" value="USER" /> <property name="password" value="PASS" /> </bean> <!--配置事務管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用fastjson做爲json解析器--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--注入JdbcTemplate--> <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!--配置視圖--> <bean id="jspView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!--配置攔截器--> <mvc:interceptors> <mvc:interceptor> <!--攔截匹配路徑的html和do文件--> <mvc:mapping path="/*/*.html" /> <mvc:mapping path="/*/*.do" /> <!--放過部分請求--> <mvc:exclude-mapping path="/home/login.html" /> <mvc:exclude-mapping path="/home/logout.html" /> <!--自定義的攔截器--> <bean class="com.nids.web.ActionInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>