gson json转map_浅析Gson与Spring Boot

3311010fbc5e82a10dad9e97b0fe02af.png

在本文中,我们将学习如何在Spring Boot中使用Google Gson。Gson是一个开源Java库,用于将Java对象序列化和反序列化为JSON。

Spring Boot使用Jackson作为默认库,将Java对象序列化和反序列化为JSON。如果 在应用程序中添加“ spring-boot-starter ” ,它将包含在您的类路径中。这很棒,但有时您可能希望使用其他API,而不是Spring Boot自动配置中可用的API 。在本文中,我们将介绍使用Gson和Spring Boot的步骤。

Spring Boot是一个具有某些默认值的智能系统,它具有Gson的自动配置功能。一旦发现Gson在类路径上,Spring Boot将自动配置Gson bean。它还在application.properties文件中提供了几个Gson特定属性。

1. Maven依赖

我们配置的第一步是在我们的pom.xml文件中添加Gson依赖项。这就是我们的pom.xml文件的样子:

com.google.code.gson

gson

2.8.5

通过上面的配置,Spring Boo创建了一个具有合理默认值的Gson bean。Spring提供了一个GsonHttpMessageConverter ,它可以使用Google Gson库读写JSON。

1.1使用Gson作为默认Mapper

我们将Gson包含在类路径中,但我们需要使用application.properties文件将Gson设置为首选映射器。

spring.http.converters.preferred-json-mapper=gson #Preferred JSON mapper to use for HTTP message conversion.

如果您没有设置首选的json映射器,您可能会碰到

org.springframework.http.converter.HttpMessageNotWritableException.

1.2 Gson配置

Spring Boot为Gson配置提供了几个属性。这是列表参考:

# 序列化日期对象时使用的格式。

spring.gson.date-format=

# 是否禁用HTML字符转义,如“”等。

spring.gson.disable-html-escaping=

# 是否在序列化期间排除内部类。

spring.gson.disable-inner-class-serialization=

# 是否启用复杂映射键(即非原语)的序列化。

spring.gson.enable-complex-map-key-serialization=

# 是否排除所有没有“expose”注释的字段进行序列化或反序列化。

spring.gson.exclude-fields-without-expose-annotation=

# 在序列化和反序列化期间应用于对象字段的命名策略。

spring.gson.field-naming-policy=

# 是否通过在输出前添加一些特殊文本来生成不可执行的JSON。

spring.gson.generate-non-executable-json=

# 对于解析不符合RFC 4627的JSON是否宽容。

spring.gson.lenient=

# 长类型和长类型的序列化策略。

spring.gson.long-serialization-policy=

# 是否输出适合漂亮打印页面的序列化JSON。

spring.gson.pretty-printing=

# 是否序列化空字段。

spring.gson.serialize-nulls=

d94e53045fcd3214ac88e0bc1aad8811.png

2.排除Jackson的依赖

如果您将Gson用作默认库,请从类路径中删除Jackson。有两种方法可以将它从类路径中排除

2.1使用Maven

最简单的方法是使用您的排除标记pom.xml。Spring Boot将Jackson添加为Web启动器的一部分,我们所需要的只是将其排除在Web启动器中。

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-json

com.google.code.gson

gson

2.8.5

2.1使用Exclude属性

第二种方法是使用带有@EnableAutoConfiguration或 @SpringBootApplication 的exclude属性

@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})

public class GsonSpringBootApplication {

public static void main(String[] args) {

SpringApplication.run(GsonSpringBootApplication.class, args);

}

}

使用此选项,您可以跳过设置, spring.http.converters.preferred-json-mapper因为Spring Boot只配置一个映射器

3.使用HttpMessageConverters自定义

要在Spring Boot应用程序中自定义Gson映射器的行为,您可以扩展 WebMvcConfigurerAdapter 以获取带有Spring的Http消息转换器。我们将举例说明我们要为JSON转换器自定义日期格式。

@Configuration

public class ApplicationConfig extends WebMvcConfigurerAdapter {

@Override

public void configureMessageConverters(List> converters) {

converters.add(customGsonHttpMessageConverter());

super.configureMessageConverters(converters);

}

private GsonHttpMessageConverter customGsonHttpMessageConverter() {

Gson gson = new GsonBuilder()

.excludeFieldsWithoutExposeAnnotation()

.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'")

.create();

GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();

gsonMessageConverter.setGson(gson);

return gsonMessageConverter;

}

}

4.单元测试

让我们设置一个小单元测试用例来测试Gson转换器。

@RunWith(SpringRunner.class)

@SpringBootTest

public class GsonSpringBootApplicationTests {

private Product product;

@Before

public void setup(){

product =new Product("123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值