springboot自定义消息转换器HttpMessageConverter Spring Boot - 使用Gson替换Jackson
Jackson一直是springframework默认的json库,从4.1开始,springframework支持通过配置GsonHttpMessageConverter的方式使用Gson。
在典型的Spring MVC中,一旦请求退出@Controller,它将寻找一个视图来呈现。当指定了@RequestBody或@RestController时,我们会告诉Spring跳过这一步,将java对象通过model写入响应结果。这样做时,Spring将专门寻找一个HttpMessageConverter来执行Java对象向其它类型(通常是Json)的转换,Spring默认使用的是 MappingJackson2HttpMessageConverter,所以如果希望使用Gson来执行这种转换,只要使用GsonHttpMessageConverter替换之即可。
1.为什么要使用Gson
- 速度快,效率高
粗略测试下来,执行效率 gson > fastjson > jackson - 对kotlin的支持更友好
Gson、Jackson、Fastjson这3种常见的Json库中,仅有Gson能兼容kotlin,其它2种均会使kotlin中is开头的字段在Json序列化后丢失。
2.Spring Boot项目中如何使用Gson作为Spring MVC的序列化工具
下面展示具体的实现步骤,其中还包括时间格式设置、兼容swagger(swagger默认使用jackson作为序列化工具,如果不作处理会出错)
Step 1:引入gson依赖
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.4'
Step 2:向HttpMessageConverters中添加GsonHttpMessageConverter,默认会添加到HttpMessageConverters列表的最前面以优先使用
import com.google.gson.*
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.http.HttpMessageConverters
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.json.GsonHttpMessageConverter
import springfox.documentation.spring.web.json.Json
import java.lang.reflect.Type
@Configuration
class GsonConfig {
@Value("\${spring.gson.date-format}")
private lateinit var GSON_DATE_FORMAT: String
@Bean
fun gson(): Gson {
return GsonBuilder().setDateFormat(GSON_DATE_FORMAT).registerTypeAdapter(Json::class.java, SpringfoxJsonToGsonAdapter()).create()
}
@Bean
fun httpMessageConverters(): HttpMessageConverters {
val gsonHttpMessageConverter = GsonHttpMessageConverter()
gsonHttpMessageConverter.gson = gson()
return HttpMessageConverters(true, listOf(gsonHttpMessageConverter))
}
}
internal class SpringfoxJsonToGsonAdapter : JsonSerializer<Json> {
override fun serialize(json: Json, type: Type, context: JsonSerializationContext): JsonElement = JsonParser().parse(json.value())
}
其中GSON_DATE_FORMAT指定了日期时间格式
spring:
gson:
date-format: yyyy-MM-dd HH:mm:ss
完成上述2步后,使用@RestController或者@ResponseBody时执行序列化就是由Gson来完成的了。
参考文章:https://www.leveluplunch.com/java/tutorials/023-configure-integrate-gson-spring-boot/
链接:https://www.jianshu.com/p/b2b6ba67dfb8
springboot自定义消息转换器HttpMessageConverter Spring Boot - 使用Gson替换Jackson的更多相关文章
- springboot自定义消息转换器HttpMessageConverter
在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换,底层这种灵活的消息转换机制就是利用HttpMessageCo ...
- SpringBoot启动方式,Spring Boot 定义系统启动任务
SpringBoot启动方式,Spring Boot 定义系统启动任务 SpringBoot启动方式 1.1 方法一 1.2 方法二 1.2.1 start.sh 1.2.2 stop.sh 1.2. ...
- spring boot 是如何利用jackson进行序列化的?
接上一篇:spring boot 是如何利用jackson进行反序列化的? @RestController public class HelloController { @RequestMapping ...
- SpringBoot 消息转换器 HttpMessageConverter
1.简介: Spring在处理请求时,由合适的消息转换器将请求报文绑定为方法中的形参对象,在这里,同一个对象就有可能出现多种不同的消息形式,比如json和xml.同样,当响应请求时,方法的返回值也同样 ...
- 让你的spring-boot应用日志随心所欲--spring boot日志深入分析
1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配 ...
- 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)
第一步:添加springboot的配置文件 首先我先贴出我的配置 添加依赖包 <!-- spring boot devtools 依赖包. --> <dependency> & ...
- springboot(十七):使用Spring Boot上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...
- SpringBoot学习笔记(2) Spring Boot的一些配置
外部配置 Spring Boot允许使用properties文件.yaml文件或者命令行参数作为外部配置 使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Enviro ...
- springboot成神之——spring boot,spring jdbc和spring transaction的使用
本文介绍spring boot,spring jdbc和spring transaction的使用 项目结构 依赖 application model层 mapper层 dao层 exception层 ...
随机推荐
- idea2017显示maven Project菜单
右侧就出现 maven project菜单了.
- Multihypothesis Trajectory Analysis for Robust Visual Tracking
Multihypothesis Trajectory Analysis for Robust Visual Tracking 2019-10-27 14:33:49 Paper: https://ww ...
- vue npm run dev 报错 semver\semver.js:312 throw new TypeError('Invalid Version: ' + version)
npm run dev运行报错信息如下图: 原因分析: 版本问题 解决办法: 在semver.js(node_modules/semver/semver.js)里做了一些改动,代码如下: // if ...
- paddlepaddle如何预加载embedding向量
使用小批量数据时,模型容易过拟合,所以需要对全量数据进行处理,我是用的是word2vec训练的词向量. 那么训练好对词向量如何加载呢? #!/usr/bin/env python # -*- codi ...
- Spark动态资源分配-Dynamic Resource Allocation
微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理.OLAP分析与可视化平台 | 赞助作者:赞助作者 Spark动态资源分配-Dynamic Resource Allocation S ...
- docker部署spring boot项目在服务器上
IDE:idea 工具:docker spring boot:2.0.1 ======================================== 简单记录一下流程,以供参考: 第一步:首先得 ...
- 使用 Visual Studio Code(VSCode)搭建简单的Python+Django开发环境的方法步骤
安装配置 VSCode [1]安装 VSCode: 下载地址:https://code.visualstudio.com/ 根据自己电脑对应的操作系统下载对应的版本即可,至于安装过程也和一般的软件一样 ...
- React 简介
- EF的 NoTracking 的一些记录
NoTracking官方解释 跟踪与非跟踪查询 跟踪行为可控制 Entity Framework Core 是否将有关实体实例的信息保留在其更改跟踪器中. 如果已跟踪某个实体,则该实体中检测到的任何更 ...
- 使用Android手机进行开发的尝试
使用Android手机查看和修改Excel文件.PowerPoint文件并连接幻灯机进行演示等办公方式想必大家已经有所了解.今天介绍一下怎样使用Android进行软件开发. Termux 使用Andr ...