本文主要详细介绍一下FastJson与SSM和SpringBoot的集成,在使用fastjson时不但要引入fastjson的jar包还要对其进行全局配置,才能完成对fastjson的集成。
本文使用的fastjson版本为1.2.47
本文使用的SpringBoot版本为2.0.1
1.FastJson与SSM的集成
1.1集成代码
配置写在springMVC的xml文件里,直接上代码: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<!-- Configures the programming model 必须加上这个,不然请求controller时会出现no mapping url错误-->
<!-- annotation-driven 控制器映射器和控制器适配器 ,用来控制处理http请求的方式-->
<mvc:annotation-driven>
<mvc:message-converters><!-- register-defaults="true"表示使用默认的消息转换器,默认为true-->
<!-- FastJson(Spring4.2x以上版本设置) -->
<!-- 使用注解并且返回值类型为String时,返回的string字符串带有双引号"{'user':'songfs'}",-->
<!-- 其原因是直接将string类型转成了json字符串,应该在json解析器之前添加字符串解析器-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<!-- 加入支持的媒体类型 -->
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然IE执行AJAX时,返回JSON会出现下载文件 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- FastJsonHttpMessageConverter 使支持返回Map<String,Object>等类型,它会自动转换为json-->
<!-- 需要返回json时需要配置 produces = "application/json"。不需要再指定utf-8了 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 加入支持的媒体类型 -->
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然IE执行AJAX时,返回JSON会出现下载文件 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
<!--SerializerFeature属性使用-->
<property name="features">
<list>
<!--前三个比较常用-->
<!--是否输出值为null的字段,默认为false-->
<value>WriteMapNullValue</value>
<!--字符类型字段如果为null,输出为"",而非null,默认为false-->
<value>WriteNullStringAsEmpty</value>
<!--List字段如果为null,输出为[],而非null,默认为false-->
<value>WriteNullListAsEmpty</value>
<!--数值字段如果为null,输出为0,而非null(包括Integer类型,如果为null,输出为0),默认为false-->
<value>WriteNullNumberAsZero</value>
<!--Boolean字段如果为null,输出为false,而非null,默认为false-->
<value>WriteNullBooleanAsFalse</value>
<!--全局修改日期格式,默认为false,格式为yyyy-MM-dd HH:mm:ss-->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
1.2集成详细介绍
1.2.1StringHttpMessageConverter
1)如果是一个JSON字符串”{\”aaa\”:\”你好\”}”,只使用FastJsonHttpMessageConverter会原样输出一个JSON字符串,但是在这之前加了一个StringHttpMessageConverter就会输出一个JSON对象{“aaa”:”你好”}。
2)如果JSON字符串中含有中文会出现乱码问题,因为从org.springframework.http.converter.StringHttpMessageConverter进入源码中会发现默认使用的是ISO-8859-1编码格式,为保持编码统一设置为UTF-8编码格式。这样不会出现中文乱码问题。
1.2.2FastJsonHttpMessageConverter
1)从com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter进入源码会发现默认使用的是UTF-8编码格式,所以在supportedMediaTypes中不设置编码格式也可以。
2)SerializerFeature属性可以快速设置改变输出的样式。
2.FastJson与SpringBoot的集成
springboot配置如下: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
44import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class WebConfig extends WebMvcConfigurationSupport {
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.定义一个convert转换消息对象--字符串解析器
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
//2.将convert添加到converters中并设置优先级
converters.add(0,converter);
//1.定义一个convert转换消息对象--json解析器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.1添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteDateUseDateFormat);
//2.2处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//4.将convert添加到converters中并设置优先级
converters.add(1,fastConverter);
}
}
在springboot中配置fastjson不会写在xml中,会创建一个配置类进行fastjson的配置,需要创建两个自定义消息转化器,分别为StringHttpMessageConverter和FastJsonHttpMessageConverter,并且两个自定义消息转换器是有优先级的,优先级按上述代码中的设置为准。
相关博客推荐:https://www.cnblogs.com/hellxz/p/8735602.html
https://pre.iteye.com/blog/2438878