码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC4设置使用fastjson作为json解析器,替代jackson

时间:2016-05-09 11:10:50      阅读:5453      评论:0      收藏:0      [点我收藏+]

标签:spring

不论是性能、易用性、特性支持,fastjson都要远好于默认的jackson,所以如果应用程序经常使用ajax进行数据交互,建议用fastjson作为默认解析器,只需要简单配置:

<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的包就好了。


附Spring MVC4示例配置文件:

<?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>




本文出自 “兔子窝” 博客,请务必保留此出处http://boytnt.blog.51cto.com/966121/1771390

Spring MVC4设置使用fastjson作为json解析器,替代jackson

标签:spring

原文地址:http://boytnt.blog.51cto.com/966121/1771390

(0)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!