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层 ...
随机推荐
- [Beta阶段]第五次Scrum Meeting
Scrum Meeting博客目录 [Beta阶段]第五次Scrum Meeting 基本信息 名称 时间 地点 时长 第五次Scrum Meeting 19/05/10 新主楼F座2楼 50min ...
- No Suitable Driver Found For Jdbc
今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是三个问题: 一是:连接URL格式出现了问题(Connection conn ...
- SVN提示is already locked 解决办法
当svn提示is already locked ,反复clean up也无用, 可以在cmd下进入到目标文件夹的目录 执行svn cleanup 等待执行成功,就可以update了
- Unity资源商店 Asset store下载文件夹的位置
Win10 C:\Users\用户名\AppData\Roaming\Unity\Asset Store-5.x\ Mac OS X ~/Library/Unity/Asset Store
- 【Swoole】计一次swoole_server配合laravel5启动报错:Address already in use[98]
[2019-11-11 11:42:25 @21371.0] WARNING swSocket_bind(:434): bind(0.0.0.0:9501) failed, Error: Addre ...
- springboot vue前后端分离 跨跨域配置
public class CustomCorsFilter extends OncePerRequestFilter { @Override protected void doFilterIntern ...
- promise 和 async await比较
async搭配await是ES7提出的,它的实现是基于Promise.这里使用它对比Promise的用法,这里只是简单的适合日常业务的使用场景. async.await是ES7中的提案,通过同步方 ...
- Win10如何开启蓝屏记录?Win10开启蓝屏信息记录的方法
转载:http://www.xitongzhijia.net/xtjc/20170127/91010.html 蓝屏,是电脑最常见的故障,一般出现蓝屏时都会显示详细的蓝屏错误信息,方便用户排查故障.最 ...
- python中的__futrue__模块,以及unicode_literals模块
Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了. 从Python 2.7到Pytho ...
- mysqldump导出完整sql脚本
#导出某个数据库--结构+数据 shell>mysqldump -h192.168.161.124 -uroot -pxxxxxx --opt db_name |gzip -9 > /db ...