Spring Boot 3.x特性-JSON(gson,jackson,json-b,fastjson)

系列文章目录

系列文章:Spring Boot 3.x 系列教程



前言

Spring Boot提供了与三个JSON映射库的集成:

  • Gson
  • Jackson
  • JSON-B

Jackson 是首选的默认库。

Spring Boot 3.x- Servlet Web应用程序开发(Spring MVC)文中有介绍过:

Spring MVC使用HttpMessageConverter接口来转换HTTP请求和响应。默认情况下是开箱即用,例如,对象可以自动转换为JSON(通过使用Jackson库)或XML(如果可用,则使用Jackson XML扩展;如果Jackson XML扩展不可用,则使用JAXB),默认情况下,字符串以UTF-8编码。

Spring Boot自带的JSON格式转换,HttpMessageConverter实现有如下几种:

1.MappingJackson2HttpMessageConverter(默认)

2.JsonbHttpMessageConverter

3.GsonHttpMessageConverter

可以使用属性spring.mvc.converters.preferred-json-mapper选择具体的josn(jackson,gson,jsonb)转换方式。


一、Jackson

Jacksonspring-boot-starter-json的一部分, 提供了Jackson的自动配置。当Jackson位于类路径中时,将自动配置ObjectMapper。Spring Boot并且提供了几个配置属性,用于定制ObjectMapper的配置。

当引入spring-boot-starter-web依赖时,同时包含了spring-boot-starter-json。所以一般无需单独的引入。

在这里插入图片描述
下面例子使用默认的Jackson,使接口输出内容为json格式。

引入web依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

新建接口:

@RestController
public class TestJsonController {
    @RequestMapping(value = "/user/detail")
    public User user() {
        return new User(1024l, "hello", "word");
    }
    @Data
    @AllArgsConstructor
    class User implements Serializable {
        private Long id;
        private String firstName;
        private String lastName;
    }
}

访问接口输出以下内容:
在这里插入图片描述

无需任何的额外配置就可以集成Jackson,这些都归根于Spring Boot的自动配置:

Spring MVC(客户端和服务器端)使用HttpMessageConvertersHTTP交换中协商内容转换。如果Jackson位于类路径中,则已经获得了Jackson2ObjectMapperBuilder提供的默认转换器,该转换器的实例已为你自动配置。

ObjectMapper(XmlMapper)实例(默认创建)具有以下自定义属性:

  1. MapperFeature.DEFAULT_VIEW_INCLUSION 禁用
  2. DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES 禁用
  3. SerializationFeature.WRITE_DATES_AS_TIMESTAMPS 禁用

你可以通过使用环境配置ObjectMapperXmlMapper实例。Jackson提供了一套广泛的开/关特性,可用于配置其处理的各个方面。这些特性在6个枚举中描述(在Jackson中),它们映射到环境中的属性:

枚举属性说明
com.fasterxml.jackson.databind.SerializationFeaturespring.jackson.serialization.<feature_name>true, false序列化特性开关
com.fasterxml.jackson.databind.DeserializationFeaturespring.jackson.deserialization.<feature_name>true, false反序列化特性开关
com.fasterxml.jackson.core.JsonGenerator.Featurespring.jackson.generator.<feature_name>true, falseJsonGenerator可切换特性开关
com.fasterxml.jackson.databind.MapperFeaturespring.jackson.mapper.<feature_name>true, falseObjectMapper特性开关
com.fasterxml.jackson.core.JsonParser.Featurespring.jackson.parser.<feature_name>true, falseJsonParser特性开关
com.fasterxml.jackson.annotation.JsonInclude.Includespring.jackson.default-property-inclusionalways, non_null, non_absent, non_default, non_empty定义在序列化中包含Java Bean的哪些属性

常用属性配置:

# 属性命名策略,PropertyNamingStrategy常量,SNAKE_CASE驼峰转下划线
spring.jackson.property-naming-strategy=SNAKE_CASE
# @JsonFormat的格式转换
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#设置全局时区
spring.jackson.time-zone=GMT+8
#属性null值处理方式,非空才序列化
spring.jackson.default-property-inclusion=non_null
#枚举类SerializationFeature
#Date转换成timestamp
spring.jackson.serialization.write-dates-as-timestamps=true
#对象为null报错
spring.jackson.serialization.fail-on-empty-beans=true
#枚举类DeserializationFeature
#反序列化不存在属性时是否报错,默认true
spring.jackson.deserialization.fail-on-unknown-properties=false
#使用getter取代setter探测属性,如类中含getName()但不包含name属性与setName(),json输出包含name。默认false
spring.jackson.mapper.use-getters-as-setters=true
#枚举类JsonParser.Feature
#是否允许出现单引号,默认false
spring.jackson.parser.allow-single-quotes=true

二、Gson

Gson在类路径上时,会自动配置Gson Beanspring.gson.*提供自定义配置属性。要获得更多的控制,可以使用一个或多个GsonBuilderCustomizer Bean

引入依赖,需要排除默认的jackson依赖,然后加入gson依赖:

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <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>
        </dependency>

常用配置

# 序列化Date对象时使用的格式。
spring.gson.date-format=yyyy-MM-dd HH:mm:ss
#是否禁用HTML字符转义,如'<','>'等。
spring.gson.disable-html-escaping=true
#是否在序列化过程中排除内部类。
spring.gson.disable-inner-class-serialization=false
#是否启用复杂映射键(即非原语)的序列化
spring.gson.enable-complex-map-key-serialization=false
#在序列化或反序列化时,是否排除所有没有\"Expose\"注释的字段
spring.gson.exclude-fields-without-expose-annotation=true
#在序列化和反序列化期间应用于对象字段的命名策略 FieldNamingPolicy
spring.gson.field-naming-policy=UPPER_CAMEL_CASE
#是否通过在输出前加上一些特殊文本来生成不可执行的JSON。
spring.gson.generate-non-executable-json=false
#是否对不符合RFC 4627的JSON进行宽容的解析。
spring.gson.lenient=false
# Long类型和long类型的序列化策略。LongSerializationPolicy
spring.gson.long-serialization-policy=DEFAULT
# 是否输出适合页面的序列化JSON以进行漂亮的打印。
spring.gson.pretty-printing=true
# 是否序列化空字段。
spring.gson.serialize-nulls=true

三、JSON-B

JSON-B API和实现在类路径上时,将自动配置一个Json Bean。首选的JSON-B实现是Eclipse Yasson,它提供了依赖项管理。

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.eclipse</groupId>
            <artifactId>yasson</artifactId>
            <version>2.0.4</version>
        </dependency>

四、Fastjson

fastjson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 JavaBean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 JavaBean

因为fastjson不是Spring Boot官方默认的json库,因此需要自定义HttpMessageConverterfastjosn默认提供了跟spring集成的HttpMessageConverter实现:FastJsonHttpMessageConverter
引入依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>
public class FastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> implements GenericHttpMessageConverter<Object> {
  }

所以只需要自动创建FastJsonHttpMessageConverter,覆盖默认的HttpMessageConverter

@Configuration
public class FastJsonGlobalConfig {
    @Bean
    FastJsonHttpMessageConverter getconvers(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        jsonConfig.setCharset(Charset.forName("UTF-8"));
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(jsonConfig);
        converter.setDefaultCharset(Charset.forName("Utf-8"));
        return converter;
    }
}

还可以使用WebMvcConfigurer方式配置:

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        jsonConfig.setCharset(Charset.forName("UTF-8"));
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(jsonConfig);
        converter.setDefaultCharset(Charset.forName("Utf-8"));
        converters.add(0, converter);
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

laopeng301

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

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

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

打赏作者

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

抵扣说明:

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

余额充值