EN
Spring Boot 2 - configure Fastjson as default JSON library (Jackson replacement)
9 points
In this short article, we would like to show how to use Alibaba Fastjson library in Spring Boot 2 (or 3 also).
By default Spring Boot 2/3 uses Jackson library to convert Java objects to JSONs and back, so there are needed to do some changes in configurations.
It is necessary to:
- exclude default Jackson library,
- include Fastjson library,
- include Fastjson Spring extension library,
- add
FastJsonHttpMessageConverter
configuration.
In pom.xml
file do the following changes:
xxxxxxxxxx
1
<!-- ... -->
2
3
<dependencies>
4
<!-- ... -->
5
<dependency>
6
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
7
<groupId>org.springframework.boot</groupId>
8
<artifactId>spring-boot-starter-web</artifactId>
9
<exclusions>
10
<exclusion> <!-- ADD IT -->
11
<groupId>com.fasterxml.jackson.core</groupId> <!-- ADD IT -->
12
<artifactId>jackson-core</artifactId> <!-- ADD IT -->
13
</exclusion> <!-- ADD IT -->
14
<exclusion> <!-- ADD IT -->
15
<groupId>com.fasterxml.jackson.core</groupId> <!-- ADD IT -->
16
<artifactId>jackson-databind</artifactId> <!-- ADD IT -->
17
</exclusion> <!-- ADD IT -->
18
<exclusion> <!-- ADD IT -->
19
<groupId>com.fasterxml.jackson.core</groupId> <!-- ADD IT -->
20
<artifactId>jackson-annotations</artifactId> <!-- ADD IT -->
21
</exclusion> <!-- ADD IT -->
22
</exclusions>
23
</dependency>
24
<!-- ... -->
25
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
26
<dependency> <!-- ADD IT -->
27
<groupId>com.alibaba.fastjson2</groupId> <!-- ADD IT -->
28
<artifactId>fastjson2</artifactId> <!-- ADD IT -->
29
<version>2.0.51</version> <!-- ADD IT -->
30
</dependency> <!-- ADD IT -->
31
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 -->
32
<dependency> <!-- ADD IT -->
33
<groupId>com.alibaba.fastjson2</groupId> <!-- ADD IT -->
34
<artifactId>fastjson2-extension-spring6</artifactId> <!-- ADD IT -->
35
<version>2.0.51</version> <!-- ADD IT -->
36
</dependency> <!-- ADD IT -->
37
<!-- ... -->
38
<dependencies>
39
40
<!-- ... -->
Add JsonMapperConfig.java
file to application configurations:
xxxxxxxxxx
1
package com.example.config;
2
3
import java.nio.charset.StandardCharsets;
4
import java.util.Arrays;
5
import java.util.List;
6
7
import org.springframework.context.annotation.Bean;
8
import org.springframework.context.annotation.Configuration;
9
import org.springframework.http.MediaType;
10
import org.springframework.http.converter.HttpMessageConverter;
11
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12
13
import com.alibaba.fastjson2.support.config.FastJsonConfig;
14
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
15
16
17
public class JsonMapperConfig implements WebMvcConfigurer {
18
19
20
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
21
var config = new FastJsonConfig();
22
config.setCharset(StandardCharsets.UTF_8);
23
var converter = new FastJsonHttpMessageConverter();
24
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
25
converter.setFastJsonConfig(config);
26
return converter;
27
}
28
29
30
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
31
converters.add(this.fastJsonHttpMessageConverter());
32
}
33
}
Manually in our source code we are able to convert:
1. Objects to JSONs by:
xxxxxxxxxx
1
// import com.alibaba.fastjson2.JSON;
2
3
SomeClass object = ...
4
String json = JSON.toJSONString(object);
2. JSONs to Objects by:
xxxxxxxxxx
1
// import com.alibaba.fastjson2.JSON;
2
3
String json = ...;
4
SomeClass object = JSON.parseObject(json, SomeClass.class);
Alternative titles
- Spring Boot 3 - configure Fastjson as default JSON library (as default Jackson replacement)
- Spring Boot 2 - use Fastjson as default JSON library (as default Jackson replacement)
- Spring Boot 3 - use Fastjson as default JSON library (as default Jackson replacement)
- Spring Boot 2 - configure Alibaba Fastjson as default JSON library (Jackson replacement)