JSON
Spring Boot 提供了与三个 JSON 映射库的集成:
Spring Boot provides integration with three JSON mapping libraries:
-
Gson
-
Jackson
-
JSON-B
Jackson 是首选和默认库。
Jackson is the preferred and default library.
Jackson
提供了 Jackson 的自动配置,而 Jackson 是 spring-boot-starter-json
的一部分。当 Jackson 在类路径上时,将自动配置一个 ObjectMapper
Bean。为 customizing the configuration of the ObjectMapper
提供了多个配置属性。
Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json
.
When Jackson is on the classpath an ObjectMapper
bean is automatically configured.
Several configuration properties are provided for customizing the configuration of the ObjectMapper
.
Custom Serializers and Deserializers
如果你使用 Jackson 对 JSON 数据进行序列化和反序列化,你可能需要编写自己的 JsonSerializer
类和 JsonDeserializer
类。自定义序列化器通常是 registered with Jackson through a module,但 Spring Boot 提供了一个替代的 @JsonComponent
批注,可以更容易地直接注册 Spring Bean。
If you use Jackson to serialize and deserialize JSON data, you might want to write your own JsonSerializer
and JsonDeserializer
classes.
Custom serializers are usually registered with Jackson through a module, but Spring Boot provides an alternative @JsonComponent
annotation that makes it easier to directly register Spring Beans.
你可以对 JsonSerializer
、JsonDeserializer
或 KeyDeserializer
实现直接使用 @JsonComponent
批注。你还可以将其用于包含序列化器/反序列化器的类,这在以下示例中有所体现:
You can use the @JsonComponent
annotation directly on JsonSerializer
, JsonDeserializer
or KeyDeserializer
implementations.
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:
ApplicationContext
中的所有 @JsonComponent
Bean 会自动向 Jackson 注册。由于 @JsonComponent
的元批注是 @Component
,所以会应用通常的组件扫描规则。
All @JsonComponent
beans in the ApplicationContext
are automatically registered with Jackson.
Because @JsonComponent
is meta-annotated with @Component
, the usual component-scanning rules apply.
Spring Boot 还会提供 {code-spring-boot-src}/jackson/JsonObjectSerializer.java[JsonObjectSerializer
] 和 {code-spring-boot-src}/jackson/JsonObjectDeserializer.java[JsonObjectDeserializer
] 基本类,在序列化对象时,这些类提供对标准 Jackson 版本的有用替代品。有关详细信息,请参阅 Javadoc 中的 JsonObjectSerializer
和 JsonObjectDeserializer
。
Spring Boot also provides {code-spring-boot-src}/jackson/JsonObjectSerializer.java[JsonObjectSerializer
] and {code-spring-boot-src}/jackson/JsonObjectDeserializer.java[JsonObjectDeserializer
] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
See JsonObjectSerializer
and JsonObjectDeserializer
in the Javadoc for details.
上面的示例可以重新编写,这样使用 JsonObjectSerializer
/JsonObjectDeserializer
:
The example above can be rewritten to use JsonObjectSerializer
/JsonObjectDeserializer
as follows:
Mixins
Jackson 支持 mixin,可用于将额外的批注混合到目标类已经声明的批注中。Spring Boot 的 Jackson 自动配置会扫描你的应用程序包来查找使用 @JsonMixin
批注的类,并将其向自动配置的 ObjectMapper
注册。注册是通过 Spring Boot 的 JsonMixinModule
执行的。
Jackson has support for mixins that can be used to mix additional annotations into those already declared on a target class.
Spring Boot’s Jackson auto-configuration will scan your application’s packages for classes annotated with @JsonMixin
and register them with the auto-configured ObjectMapper
.
The registration is performed by Spring Boot’s JsonMixinModule
.
Gson
提供了 Gson 的自动配置。当 Gson 处于 classpath 上时,会自动配置一个 Gson
Bean。提供了几个 spring.gson.*
配置属性来定制配置。为了获得更多控制,可以使用一个或多个 GsonBuilderCustomizer
Bean。
Auto-configuration for Gson is provided.
When Gson is on the classpath a Gson
bean is automatically configured.
Several spring.gson.*
configuration properties are provided for customizing the configuration.
To take more control, one or more GsonBuilderCustomizer
beans can be used.
JSON-B
提供了 JSON-B 的自动配置。当 JSON-B API 和一个实现位于 classpath 上时,将自动配置一个 Jsonb
Bean。优选的 JSON-B 实现是 Eclipse Yasson,并为此提供了依赖项管理。
Auto-configuration for JSON-B is provided.
When the JSON-B API and an implementation are on the classpath a Jsonb
bean will be automatically configured.
The preferred JSON-B implementation is Eclipse Yasson for which dependency management is provided.