不论是性能、易用性、特性支持,fastjson都要远好于默认的jackson,所以如果应用程序经常使用ajax进行数据交互,建议用fastjson作为默认解析器,只需要简单配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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示例配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<? 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 >
|
本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/1771390,如需转载请自行联系原作者