【Spring Boot丨(11 )】json的集成

在这里插入图片描述

主页传送门:📀 传送

概述


Spring boot 提供了三种json库的集成:

  • Gson
  • Jackson
  • JSON-B

上述三种库提供了将Java对象转换为JSON字符串以及将JSON字符串转换为Java对象的功能。
其中Jackson 是 Spring Boot 官方的首选推荐和默认库。

Jackson


  Spring Boot 提供了 Jackson的自动配置,并且Jacksonspring-boot-starter-json 的一部分。当 Jackson 在类路径上时,会自动配置 ObjectMapper bean

自定义配置属性:

ObjectMapper具有以下自定义属性:

  • MapperFeature.DEFAULT_VIEW_INCLUSION 被禁用

  • DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES 被禁用

  • SerializationFeature.WRITE_DATES_AS_TIMESTAMPS 被禁用

  Spring Boot 还有一些功能可以更轻松地自定义此行为。使用环境配置ObjectMapper和XmlMapper实例。 Jackson

供了一套广泛的开/关功能,可用于配置其处理的各个方面。这些特征在六个枚举(Jackson中)中进行了描述,这些枚举映射到

环境中的属性如下:

枚举属性
com.fasterxml.jackson.databind.DeserializationFeaturespring.jackson.deserialization.<feature_name>true,false
com.fasterxml.jackson.core.JsonGenerator.Featurespring.jackson.generator.<feature_name>true,false
com.fasterxml.jackson.databind.MapperFeaturespring.jackson.mapper.<feature_name>true,false
com.fasterxml.jackson.core.JsonParser.Featurespring.jackson.parser.<feature_name>true,false
com.fasterxml.jackson.databind.SerializationFeaturespring.jackson.serialization.<feature_name>true,false
com.fasterxml.jackson.annotation.JsonInclude.Includespring.jackson.default-property-inclusionalways, non_null, non_absent, non_default,non_empty

示例:

spring.jackson.date-format= // 序列化日期对象时使用的格式。日期格式字符串或完全限定的日期格式类名。例如,`yyyy-MM-dd HH:MM:ss`
spring.jackson.default-property-inclusion= // 控制序列化过程中包含的属性。使用Jackson的JsonInclude.Include枚举中的一个值配置。
spring.jackson.deserialization.*= // 影响Java对象反序列化方式的Jackson开/关特性。
spring.jackson.generator.*= // 用于Jackson发生器的开关功能。
spring.jackson.joda-date-time-format= // Joda日期时间格式字符串。如果没有配置,则如果使用格式字符串配置“日期格式”时会将其用作回退。
spring.jackson.locale= // 用于格式化的区域设置。
spring.jackson.mapper.*= // Jackson 通用开关功能。
spring.jackson.parser.*= // 解析程序的Jackson开/关功能。
spring.jackson.property-naming-strategy= //Jackson属性命名策略中的一个常数。也可以是PropertyNamingStrategy子类的完全限定类名。
spring.jackson.serialization.*= // 影响Java对象序列化方式的Jackson开/关特性。
spring.jackson.serialization.indent_output=true // 启用漂亮的打印  松散绑定原则,不需要大小写一致
spring.jackson.time-zone= // 设置日期格式时使用的时区。例如,“美国/洛杉矶”或“GMT+10”.
spring.jackson.visibility.*= // 可用于限制自动检测哪些方法(和字段)的Jackson可见性阈值。

Gson


  Spring Boot 提供了 Gson 的自动配置。当 Gson 在 classpath 上时,会自动配置 Gson bean。提供了几个 spring.gson.* 配置属性来自定义配置。为了获得更多控制,可以使用一个或多个 GsonBuilderCustomizer bean。

配置属性:

spring.gson.date-format= # 序列化日期对象时使用的格式.
spring.gson.disable-html-escaping= # 是否禁用HTML字符的转义,如“<”、“>”等。
spring.gson.disable-inner-class-serialization= # 是否在序列化期间排除内部类。
spring.gson.enable-complex-map-key-serialization= # 是否启用复杂映射键(即非基元)的序列化。
spring.gson.exclude-fields-without-expose-annotation= # 是否将所有没有“Expose”注释的字段排除在序列化或反序列化的考虑范围之外
spring.gson.field-naming-policy= # 在序列化和反序列化期间应应用于对象字段的命名策略。
spring.gson.generate-non-executable-json= # 是否通过在输出前添加一些特殊文本来生成不可执行的JSON
spring.gson.lenient= # 对于解析不符合RFC 4627JSON是否宽容。
spring.gson.long-serialization-policy= # 长类型和长类型的序列化策略。
spring.gson.pretty-printing= # 是否输出适合页面进行漂亮打印的序列化JSON。
spring.gson.serialize-nulls= # 是否序列化空字段。

JSON-B


  Spring Boot 提供了 JSON-B 的自动配置。当 JSON-B API 在 classpath 上时,将自动配置 Jsonb bean。首选的 JSON-B 实现是 Apache Johnzon,它提供了依赖关系管理。

配置属性:

JsonFormat:用于格式化输出JSON数据,可以设置日期格式、时间格式、数字格式等。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date date;

JsonField:用于控制JSON字段的名称、顺序和类型转换等。
@JsonField(name = "age_str")
private String age;
@JsonField(order = 2)
private String name;
@JsonField(type = JsonFieldType.Integer)
private String ageStr;

JsonInclude:用于控制哪些属性应该包含在序列化中。
@JsonInclude(Include.NON_NULL)
private Map<String, Object> include;

在这里插入图片描述

  如果喜欢的话,欢迎 🤞关注 👍点赞 💬评论 🤝收藏  🙌一起讨论
  你的支持就是我✍️创作的动力!					  💞💞💞

参考资料
Spring Boot json官方文档

  • 74
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 127
    评论
评论 127
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有娇妻张兔兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值