专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > JavaScript

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

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
Spring MVC4设置使用fastjson作为json解析器,替代jackson
http://www.itkeyword.com/doc/6957280713560302897/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>

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: