【Spring Boot丨(11 )】json的集成
作者:mmseoamin日期:2023-12-25

集成JSON

  • 概述
    • Jackson
    • Gson
    • JSON-B

      在这里插入图片描述

      主页传送门:📀 传送

      概述


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

      • Gson
      • Jackson
      • JSON-B

        上述三种库提供了将Java对象转换为JSON字符串以及将JSON字符串转换为Java对象的功能。

        其中Jackson 是 Spring Boot 官方的首选推荐和默认库。

        Jackson


          Spring Boot 提供了 Jackson的自动配置,并且Jackson是 spring-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.true,false
          com.fasterxml.jackson.core.JsonGenerator.Featurespring.jackson.generator.true,false
          com.fasterxml.jackson.databind.MapperFeaturespring.jackson.mapper.true,false
          com.fasterxml.jackson.core.JsonParser.Featurespring.jackson.parser.true,false
          com.fasterxml.jackson.databind.SerializationFeaturespring.jackson.serialization.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 4627的JSON是否宽容。
          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 include;
          

          在这里插入图片描述

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

          参考资料

          Spring Boot json官方文档