企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Spring Boot 2 与 Gson > 原文: [https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/](https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/) 了解将 **[Gson](https://howtodoinjava.com/gson/gson/)** 配置为 Spring Boot 应用程序作为首选 json 映射器。 默认情况下,Spring Boot 为此使用 Jackson。 ## 1\. 包括 Gson 依赖 通过添加适当的[ Gson 依赖项](https://howtodoinjava.com/gson/gson-installation-maven-gradle-jar/)在 Spring Boot 应用程序中包含 gson。 `build.gradle` ```java dependencies { compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' } ``` `pom.xml` ```java <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> ``` ## 2\. Gson 自动配置 通过包含依赖关系,Spring Boot 可以检测到对类路径的 Gson 依赖关系,并创建一个具有合理默认值的`Gson` bean。 #### 2.1. 默认 Gson 实例 Spring Boot 会检测到`Gson.class`的存在,并使用 [GsonAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java) 来配置 Gson 实例。 如果我们需要直接访问`Gson`实例,则可以直接在 spring bean 中自动装配: `Autowire Gson instance` ```java @Autowire private Gson gson; ``` #### 2.2. 自定义 Gson 实例 要自定义 Gson 实例的默认行为,请从下面的列表中配置相应的属性。 `GsonAutoConfiguration`在初始化`Gson`实例时使用这些属性。 `application.properties` ```java # Format to use when serializing Date objects. spring.gson.date-format= # Whether to disable the escaping of HTML characters such as '<', '>', etc. spring.gson.disable-html-escaping= # Whether to exclude inner classes during serialization. spring.gson.disable-inner-class-serialization= # Whether to enable serialization of complex map keys (i.e. non-primitives). spring.gson.enable-complex-map-key-serialization= # Whether to exclude all fields from consideration for serialization or deserialization that do not have the "Expose" annotation. spring.gson.exclude-fields-without-expose-annotation= # Naming policy that should be applied to an object's field during serialization and deserialization. spring.gson.field-naming-policy= # Whether to generate non executable JSON by prefixing the output with some special text. spring.gson.generate-non-executable-json= # Whether to be lenient about parsing JSON that doesn't conform to RFC 4627. spring.gson.lenient= # Serialization policy for Long and long types. spring.gson.long-serialization-policy= # Whether to output serialized JSON that fits in a page for pretty printing. spring.gson.pretty-printing= # Whether to serialize null fields. spring.gson.serialize-nulls= ``` ## 3\. 使 Gson 成为首选 json 映射器 要将 Spring Boot 配置为使用`Gson`作为首选 Jackson 映射器,请在`application.properties`文件中使用此属性。 `application.properties` ```java spring.http.converters.preferred-json-mapper=gson ``` 现在,即使 Jackson 在类路径中可用,Spring 运行也会使用 Gson 来[序列化和反序列化 JSON 载荷](https://howtodoinjava.com/gson/gson-serialize-deserialize-json/)。 ## 4\. 完全排除 Jackson 如果我们想将 Jackson 完全排除在应用程序运行时之外,可以通过排除`spring-boot-starter-json`来实现。 #### 4.1. 从项目依赖项中排除 Jackson ```java <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Exclude the default Jackson dependency --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> ``` #### 4.2. 禁用自动配置 如果我们只想在 SpringBoot 配置中排除 Jackson,可以通过禁用[自动配置](https://howtodoinjava.com/spring-boot/springbootapplication-auto-configuration/)类`JacksonAutoConfiguration`来排除它。 `SpringBootApplication.java` ```java @SpringBootApplication(exclude = {JacksonAutoConfiguration.class}) public class SpringBootApplication { public static void main(String[] args) { SpringApplication.run(SpringBootApplication.class, args); } } ``` 请问您有关**使用 Spring Boot 2** 配置 Gson 的问题。 学习愉快!