zoukankan      html  css  js  c++  java
  • SpringBoot 使用 Gson 序列化(禁用 Jackson)

    1. SpringBoot 整合 Gson

    Jackson is the preferred and default library.

    Jackson

    Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json. When Jackson is on the classpath an ObjectMapper bean is automatically configured. Several configuration properties are provided for customizing the configuration of the ObjectMapper.

    Gson

    Auto-configuration for Gson is provided. When Gson is on the classpath a Gson bean is automatically configured. Several spring.gson.* configuration properties are provided for customizing the configuration. To take more control, one or more GsonBuilderCustomizer beans can be used.

    (1) 禁用 Jackson

    去除 JacksonAutoConfiguration

    @SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
    

    去除 Jackson 依赖(可选)

    <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>
    

    (2) 使用 Gson

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
    

    配置 HttpMessageConverter

    @Configuration
    public class ApplicationConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(customGsonHttpMessageConverter());
            super.configureMessageConverters(converters);
        }
    
        private GsonHttpMessageConverter customGsonHttpMessageConverter() {
            return new GsonHttpMessageConverter(new GsonBuilder().create());
        }
    }
    

    或使用此方式(推荐

    @Configuration
    public class GsonConfig {
    
        @Bean
        public GsonHttpMessageConverter customGsonHttpMessageConverter(Gson gson) {
            return new GsonHttpMessageConverter(gson);
        }
    }
    

    (3) 验证

    至此,@ResponseBody 转化 Json 时使用的就是 GsonHttpMessageConverter

    2. 整合 Swagger

    SpringBoot 整合 Swagger3.0

    Springfox 默认使用 Jackson 做序列化。若使用 Gson,则解析的 Json 格式会有误

    @Configuration
    public class GsonConfig {
    
        @Bean
        public Gson gson() {
            return new GsonBuilder()
                    .registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter())
                    .create();
        }
    
        @Bean
        public GsonHttpMessageConverter customGsonHttpMessageConverter(Gson gson) {
            return new GsonHttpMessageConverter(gson);
        }
    
        public class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> {
            @Override
            public JsonElement serialize(Json json, Type type, JsonSerializationContext jsonSerializationContext) {
                return JsonParser.parseString(json.value());
            }
        }
    }
    

    注意:需引入 Jackson 包,否则 Swagger 会启动失败

  • 相关阅读:
    System.nanoTime()的使用
    只为高效、流畅开发 —— 码云企业版 3.0 倾情上线
    不自律的人,下场都很惨
    刘德:小米已投89家生态链企业 有品要做百亿电商平台(本质上是是利用了小米的大火炉的余热,但也有反向的正面作用)
    英雄无敌手游(战争纪元云中城,还可以骑龙,绝美)
    openFrameworks 是一个旨在助力你进行开创性工作的开源 C++ 工具箱(是很多其它类库的组合)
    Core开发-MVC 使用dotnet 命令创建Controller和View
    Ant Table组件
    web性能优化
    scss + react + webpack + es6
  • 原文地址:https://www.cnblogs.com/lb477/p/14752162.html
Copyright © 2011-2022 走看看