原文: https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/

了解将 Gson 配置为 Spring Boot 应用程序作为首选 json 映射器。 默认情况下,Spring Boot 为此使用 Jackson。

1. 包括 Gson 依赖

通过添加适当的 Gson 依赖项在 Spring Boot 应用程序中包含 gson。

build.gradle

  1. dependencies {
  2. compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
  3. }

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.google.code.gson</groupId>
  4. <artifactId>gson</artifactId>
  5. <version>2.8.5</version>
  6. </dependency>
  7. </dependencies>

2. Gson 自动配置

通过包含依赖关系,Spring Boot 可以检测到对类路径的 Gson 依赖关系,并创建一个具有合理默认值的Gson bean。

2.1. 默认 Gson 实例

Spring Boot 会检测到Gson.class的存在,并使用 GsonAutoConfiguration 来配置 Gson 实例。

如果我们需要直接访问Gson实例,则可以直接在 spring bean 中自动装配:

Autowire Gson instance

  1. @Autowire
  2. private Gson gson;

2.2. 自定义 Gson 实例

要自定义 Gson 实例的默认行为,请从下面的列表中配置相应的属性。 GsonAutoConfiguration在初始化Gson实例时使用这些属性。

application.properties

  1. # Format to use when serializing Date objects.
  2. spring.gson.date-format=
  3. # Whether to disable the escaping of HTML characters such as '<', '>', etc.
  4. spring.gson.disable-html-escaping=
  5. # Whether to exclude inner classes during serialization.
  6. spring.gson.disable-inner-class-serialization=
  7. # Whether to enable serialization of complex map keys (i.e. non-primitives).
  8. spring.gson.enable-complex-map-key-serialization=
  9. # Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation.
  10. spring.gson.exclude-fields-without-expose-annotation=
  11. # Naming policy that should be applied to an object's field during serialization and deserialization.
  12. spring.gson.field-naming-policy=
  13. # Whether to generate non executable JSON by prefixing the output with some special text.
  14. spring.gson.generate-non-executable-json=
  15. # Whether to be lenient about parsing JSON that doesn't conform to RFC 4627.
  16. spring.gson.lenient=
  17. # Serialization policy for Long and long types.
  18. spring.gson.long-serialization-policy=
  19. # Whether to output serialized JSON that fits in a page for pretty printing.
  20. spring.gson.pretty-printing=
  21. # Whether to serialize null fields.
  22. spring.gson.serialize-nulls=

3. 使 Gson 成为首选 json 映射器

要将 Spring Boot 配置为使用Gson作为首选 Jackson 映射器,请在application.properties文件中使用此属性。

application.properties

  1. spring.http.converters.preferred-json-mapper=gson

现在,即使 Jackson 在类路径中可用,Spring 运行也会使用 Gson 来序列化和反序列化 JSON 载荷

4. 完全排除 Jackson

如果我们想将 Jackson 完全排除在应用程序运行时之外,可以通过排除spring-boot-starter-json来实现。

4.1. 从项目依赖项中排除 Jackson

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <!-- Exclude the default Jackson dependency -->
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-json</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.google.code.gson</groupId>
  15. <artifactId>gson</artifactId>
  16. <version>2.8.5</version>
  17. </dependency>
  18. </dependencies>

4.2. 禁用自动配置

如果我们只想在 SpringBoot 配置中排除 Jackson,可以通过禁用自动配置JacksonAutoConfiguration来排除它。

SpringBootApplication.java

  1. @SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
  2. public class SpringBootApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringBootApplication.class, args);
  5. }
  6. }

请问您有关使用 Spring Boot 2 配置 Gson 的问题。

学习愉快!