Spring Boot 3.x 默认库 Jackson
Spring Boot 3.x 默认库 Jackson
1、前言
Spring Boot提供了与三个JSON
映射库的集成:
- Gson
- Jackson
- JSON-B
Jackson
是首选的默认库。
Spring MVC使用HttpMessageConverter接口来转换HTTP请求和响应。默认情况下是开箱即用,例如,对象可以自动转换为JSON(通过使用Jackson库)或XML(如果可用,则使用Jackson XML扩展;如果Jackson XML扩展不可用,则使用JAXB),默认情况下,字符串以UTF-8编码。
Spring Boot自带的JSON格式转换,HttpMessageConverter实现有如下几种:
MappingJackson2HttpMessageConverter (默认)
JsonbHttpMessageConverter
GsonHttpMessageConverter
可以使用属性spring.mvc.converters.preferred-json-mapper选择具体的josn(jackson,gson,jsonb)转换方式。
2、Jackson
Jackson
是spring-boot-starter-json
的一部分, 提供了Jackson
的自动配置。当Jackson
位于类路径中时,将自动配置ObjectMapper
。Spring Boot并且提供了几个配置属性,用于定制ObjectMapper
的配置。
当引入
spring-boot-starter-web
依赖时,同时包含了spring-boot-starter-json
。所以一般无需单独的引入。

2.1、案例
引入web依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
测试代码
@RestController
@RequestMapping("/jackson")
public class JsonController {
@RequestMapping(value = "user")
public User user() {
return new User(1024L, "hello",
"word",
new Date(),
LocalDateTime.now()
);
}
@Data
@AllArgsConstructor
class User implements Serializable {
private Long id;
private String firstName;
private String lastName;
private Date birthday;
private LocalDateTime createTime;
}
}
http请求
GET http://localhost:8080/fastjson2-init/jackson/user
结果
{
"id": 1024,
"firstName": "hello",
"lastName": "word",
"birthday": "2024-06-14T03:23:59.649+00:00",
"createTime": "2024-06-14T11:23:59.6497192"
}
无需任何的额外配置就可以集成Jackson
,这些都归根于Spring Boot
的自动配置:
Spring MVC
(客户端和服务器端)使用HttpMessageConverters
在HTTP
交换中协商内容转换。如果Jackson
位于类路径中,则已经获得了Jackson2ObjectMapperBuilder
提供的默认转换器,该转换器的实例已为你自动配置。
ObjectMapper(XmlMapper)
实例(默认创建)具有以下自定义属性:
MapperFeature.DEFAULT_VIEW_INCLUSION
禁用DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
禁用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
禁用
你可以通过使用环境配置ObjectMapper
和XmlMapper
实例。Jackson
提供了一套广泛的开/关特性,可用于配置其处理的各个方面。这些特性在6个枚举中描述(在Jackson中),它们映射到环境中的属性:
枚举 | 属性 | 值 | 说明 |
---|---|---|---|
com.fasterxml.jackson.databind.SerializationFeature | spring.jackson.serialization.<feature_name> | true, false | 序列化特性开关 |
com.fasterxml.jackson.databind.DeserializationFeature | spring.jackson.deserialization.<feature_name> | true, false | 反序列化特性开关 |
com.fasterxml.jackson.core.JsonGenerator.Feature | spring.jackson.generator.<feature_name> | true, false | JsonGenerator 可切换特性开关 |
com.fasterxml.jackson.databind.MapperFeature | spring.jackson.mapper.<feature_name> | true, false | ObjectMapper 特性开关 |
com.fasterxml.jackson.core.JsonParser.Feature | spring.jackson.parser.<feature_name> | true, false | JsonParser 特性开关 |
com.fasterxml.jackson.annotation.JsonInclude.Include | spring.jackson.default-property-inclusion | always, non_null, non_absent, non_default, non_empty | 定义在序列化中包含Java Bean 的哪些属性 |
常用属性配置:
# 属性命名策略,PropertyNamingStrategy常量,SNAKE_CASE驼峰转下划线
spring.jackson.property-naming-strategy=SNAKE_CASE
# @JsonFormat的格式转换
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#设置全局时区
spring.jackson.time-zone=GMT+8
#属性null值处理方式,非空才序列化
spring.jackson.default-property-inclusion=non_null
#枚举类SerializationFeature
#Date转换成timestamp
spring.jackson.serialization.write-dates-as-timestamps=true
#对象为null报错
spring.jackson.serialization.fail-on-empty-beans=true
#枚举类DeserializationFeature
#反序列化不存在属性时是否报错,默认true
spring.jackson.deserialization.fail-on-unknown-properties=false
#使用getter取代setter探测属性,如类中含getName()但不包含name属性与setName(),json输出包含name。默认false
spring.jackson.mapper.use-getters-as-setters=true
#枚举类JsonParser.Feature
#是否允许出现单引号,默认false
spring.jackson.parser.allow-single-quotes=true