Spring Boot使用Gson替换Jackson

参考网上的一些测评,得知Gson对小文件处理比较快,Jackson处理大文件比较好,而系统主要处理小文件请求,因此打算使用Gson替换默认的Jackson。
注意项目使用Maven进行项目管理,依赖的版本号均在parent POM或import POM中维护,因此下面的Maven配置无版本号。

通常情况(排除jackson依赖)

在依赖中排除所有jackson相关依赖, 如:

1
2
3
4
5
6
7
8
9
10
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>

注意所有jackson-core,jackson-databind依赖均需要排除,可使用 mvn dependency:tree查看项目依赖。
依赖排除完成的同时加入gson依赖。

1
2
3
4
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

查看spring-webmvc.jar中WebMvcConfigurationSupport.java源码

1
2
private static final boolean jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", WebMvcConfigurationSupport.class.getClassLoader()) && ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader());
private static final boolean gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", WebMvcConfigurationSupport.class.getClassLoader());
1
2
3
4
5
6
7
8
9
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    ...
    if(jackson2Present) {
        objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
        messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    } else if(gsonPresent) {
        messageConverters.add(new GsonHttpMessageConverter());
    }
}

因此系统找不到jackson依赖时,Spring MVC便会使用GsonHttpMessageConverter而不是MappingJackson2HttpMessageConverter。

特殊情况(无法排除jackson依赖)

若无法排除jackson依赖(如其他依赖需要jackson),参考上面的源码得知Spring MVC在jackson和gson依赖都存在的情况下优先使用MappingJackson2HttpMessageConverter,因此需要代码中进行调整。

1
2
3
4
5
6
7
8
9
10
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
  @Override
  public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
      converters.removeIf(httpMessageConverter -> httpMessageConverter instanceof MappingJackson2HttpMessageConverter); // 删除MappingJackson2HttpMessageConverter
      converters.add(new GsonHttpMessageConverter()); // 添加GsonHttpMessageConverter
  }
...
}

总结

以上便是我在Spring Boot中使用Gson替换Jackson的相关配置,同样的仅使用Spring MVC而未使用Spring Boot配置也类似。

使用Gson作为默认转换时,和Swagger一起使用有问题,如何要使用Swagger的话,最好不要使用Gson替换Jackson,就使用默认的Jackson

使用Jackson作为默认转换时,配置了Swagger2Configuration,Springboot项目启动的时候会报

Caused by : java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.ObjectMapper

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring boot中文文档,从安装到部署。 I. Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot 9.系统要求 9.1.Servlet容器 10.安装Spring Boot 10.1.Java Developer的安装说明 10.1.1.Maven安装 10.1.2.Gradle安装 10.2.安装Spring Boot CLI 10.2.1.手动安装 10.2.2.使用SDKMAN安装! 10.2.3.OSX Homebrew安装 10.2.4.MacPorts安装 10.2.5.命令行完成 10.2.6.Windows Scoop安装 10.2.7.快速启动Spring CLI示例 10.3.从早期版本的Spring Boot升级 11.开发您的第一个Spring Boot应用程序 11.1.创建POM 11.2.添加Classpath依赖项 11.3.编写代码 11.3.1.@RestController和@RequestMapping Annotations 11.3.2.@EnableAutoConfiguration注释 11.3.3.“主要”方法 11.4.运行示例 11.5.创建一个可执行的Jar 12.接下来要阅读的内容 III。使用Spring Boot 13.构建系统 13.1.依赖管理 13.2.Maven 13.2.1.继承Starter Parent 13.2.2.在没有父POM的情况下使用Spring Boot 13.2.3.使用Spring Boot Maven插件 13.3.Gradle 13.4.Ant 13.5.Starters 14.构建您的代码 14.1.使用“默认”包 14.2.找到主应用程序类 15.配置类 15.1.导入其他配置类 15.2.导入XML配置 16.自动配置 16.1.逐步更换自动配置 16.2.禁用特定的自动配置类 17. Spring Beans和依赖注入 18.使用@SpringBootApplication Annotation 19.运行您的应用程序 19.1.从IDE运行 19.2.作为打包应用程序运行 19.3.使用Maven插件 19.4.使用Gradle插件 19.5.热插拔 20.开发人员工具 20.1.Property默认值 20.2.自动重启 20.2.1.记录条件评估中的更改 20.2.2.不包括资源 20.2.3.观看其他路径 20.2.4.禁用重启 20.2.5.使用触发器文件 20.2.6.自定义重新启动类加载器 20.2.7.已知限制 20.3.LiveReload 20.4.全局设置 20.5.远程应用 20.5.1.运行远程客户端应用程序 20.5.2.远程更新 21.包装您的生产

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值