Fork me on GitHub

SpringBoot———使用FastJson解析数据

SpringBoot———使用FastJson解析数据

配置文件

spring boot默认使用的json解析框架是jackson,使用fastjson需要配置,首先引入fastjson依赖

pom.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
52
53
54
55
56
57
58
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.zzqd</groupId>
<artifactId>SpringBoot_Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-Child1</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>

<!-- 加入依赖,代码做修改,可以不用重新运行 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>
</project>
配置

配置有两种方式,一种是继承WebMvcConfigurerAdapter重写方法,另一种是@Bean注入第三方的json解析框架。

继承WebMvcConfigurerAdapter
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
package net.zzqd.app;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

//@EnableAutoConfiguration
//@ComponentScan("net.zzqd.controller")
//默认情况下扫描的是当前包以及当前包的子包
@SpringBootApplication(scanBasePackages="net.zzqd.controller")//组合注解
public class SpringApplications extends WebMvcConfigurerAdapter{



@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//1创建FastJson的消息转换器
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
//创建FastJson的配置对象
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
convert.setFastJsonConfig(config);
converters.add(convert);
}

public static void main(String[] args)
{
SpringApplication.run(SpringApplications.class, args);
}
}
使用Bean注入
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
package net.zzqd.app;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

//@EnableAutoConfiguration
//@ComponentScan("net.zzqd.controller")
//默认情况下扫描的是当前包以及当前包的子包
@SpringBootApplication(scanBasePackages="net.zzqd.controller")//组合注解
public class SpringApplications {




@Bean
public HttpMessageConverters fastJsonMessageConverter()
{
//1创建FastJson的消息转换器
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
//创建FastJson的配置对象
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
convert.setFastJsonConfig(config);
HttpMessageConverter<?> con = convert;
return new HttpMessageConverters(con);
}
public static void main(String[] args)
{
SpringApplication.run(SpringApplications.class, args);
}
}
测试类controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package net.zzqd.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.zzqd.pojo.Person;

@Controller
public class TestController {

@RequestMapping("/person")
@ResponseBody
public Object show()
{
Person ren = new Person();
ren.setId(1);
ren.setName("朱朱");
ren.setDate(new Date());
return ren;
}
}
实体类
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
package net.zzqd.pojo;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Person {
private int id;
private String name;
@JSONField(format="yyyy-MM-dd HH")
private Date date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}

}
配置文件

SpringBoot默认编码UTF-8,为防止响应出现乱码,在application.properties中添加

1
spring.http.encoding.force=true
运行访问
1
{ "date":"2019-07-12 15", "id":1, "name":"朱朱" }