# SpringBoot使用第三方JSON框架
Spring boot 在底层中默认内置了jackson。
而目前市面上较为常用的是fastJson框架。
以下是替换成fastJson框架的案例
# 两种替换方法
在继续往下看前请先配置fastJson的pom依赖。
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 继承重写法
- application启动类中继承WebMvcConfigurerAdapter
- 重写configureMessageConverters方法
- 添加我们自己定义的FastJson解析框架
完整代码片段
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
/**
* 添加fastJson框架到SpringBoot底层当中。
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//先定义一个convert 转换消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastJson的配置信息,比如:是否要格式化返回的json数据。
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
// 在convert 中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 将convert 添加到 converters 当中即可。
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
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
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
# Spring注入Bean方法(常用)
该方法与第一种方法类似,但是代码嵌入较为低,一般选择该方法。
完整代码片段
@SpringBootApplication
public class Application{
/**
* 添加fastJson框架到SpringBoot底层当中。
*/
@Bean
public HttpMessageConverters configureMessageConverters() {
//先定义一个convert 转换消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastJson的配置信息,比如:是否要格式化返回的json数据。
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
// 在convert 中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 多态创建一个HttpMessageConverter;
HttpMessageConverter<?> converter = fastConverter;
// 返回HttpMessageConverters并把HttpMessageConverter入参到构造方法中。
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
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
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
# FastJson常用注解
# @JSONField
该注解提供很多的配置方案
# 时间格式化
format
常用的示例
/**
* 实体类
*/
public class Pojo {
private Integer id;
//配置后按照指定时间格式进行JSON序列化
@JSONField(format = "yyyy-MM-dd HH-mm-ss")
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
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
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
# 是否序列化
serialize
/**
* 实体类
*/
public class Pojo {
private Integer id;
@JSONField(format = "yyyy-MM-dd HH-mm-ss")
private Date createTime;
//配置后在返回json数据时,该字段不返回
@JSONField(serialize=false)
private String remarks;//备注信息
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
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
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