原文: https://howtodoinjava.com/spring-boot2/gson-with-spring-boot/
了解将 Gson 配置为 Spring Boot 应用程序作为首选 json 映射器。 默认情况下,Spring Boot 为此使用 Jackson。
1. 包括 Gson 依赖
通过添加适当的 Gson 依赖项在 Spring Boot 应用程序中包含 gson。
build.gradle
dependencies {compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'}
pom.xml
<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 来配置 Gson 实例。
如果我们需要直接访问Gson实例,则可以直接在 spring bean 中自动装配:
Autowire Gson instance
@Autowireprivate Gson gson;
2.2. 自定义 Gson 实例
要自定义 Gson 实例的默认行为,请从下面的列表中配置相应的属性。 GsonAutoConfiguration在初始化Gson实例时使用这些属性。
application.properties
# 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
spring.http.converters.preferred-json-mapper=gson
现在,即使 Jackson 在类路径中可用,Spring 运行也会使用 Gson 来序列化和反序列化 JSON 载荷。
4. 完全排除 Jackson
如果我们想将 Jackson 完全排除在应用程序运行时之外,可以通过排除spring-boot-starter-json来实现。
4.1. 从项目依赖项中排除 Jackson
<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,可以通过禁用自动配置类JacksonAutoConfiguration来排除它。
SpringBootApplication.java
@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})public class SpringBootApplication {public static void main(String[] args) {SpringApplication.run(SpringBootApplication.class, args);}}
请问您有关使用 Spring Boot 2 配置 Gson 的问题。
学习愉快!
