Spring MVC4 setup uses fastjson as json parser instead of jackson

http://www.itkeyword.com/doc/6957280713560302897/spring

quote


Whether it is performance, ease of use, feature support, fastjson is much better than the default jackson, so if the application often uses ajax for data interaction, it is recommended to use fastjson as the default parser, and only need a simple configuration:
<mvc:annotation- driven>
  <mvc:message-converters register-defaults="true">
    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 
      <property name="supportedMediaTypes" value="application/json"/>
      <!--Set fastjson features-->
      <property name="features">
        <array>
          <!--Set null value to output, fastjson is closed by default-->
          <value>WriteMapNullValue</value>
          <! --Set the output date in text mode, fastjson defaults to 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" />
    <!-- let go of some requests-->

    <mvc:exclude-mapping path="/home/logout.html" />
    <!--自定义的拦截器-->
   <bean class="com.nids.web.ActionInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

</beans>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326240489&siteId=291194637