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层 ...
随机推荐
- Gamma阶段第五次scrum meeting
每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...
- 用Eclipse的maven方式创建JFinal项目
- [Web] How to Test React and MobX with Jest
转载自: https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest?utm_content=bu ...
- 6条shell小技巧,让脚本显得不再业余【转】
如何能让自己的shell显得不那么业余?下面6点实践一定有用. 画外音:本篇文章源自Google的一篇实践,抽取了部分1分钟能读完的内容,加入了一些分析. 一.以下面的语句开场 set -o noun ...
- js 计算总页数的最高效方式
js 计算总页数的最高效方式 /** * [getTotalPageNum 获取页码总数] * @param {[type]} totalRecord [总记录] * @param {[type]} ...
- Asynchronous method in while loop 构造异步调用链
Asynchronous method in while loop https://stackoverflow.com/questions/43064719/javascript-asynchrono ...
- str.replace替换变量名的字符串
网易云课堂该课程链接地址 https://study.163.com/course/courseMain.htm?share=2&shareId=400000000398149&cou ...
- sumdoc t411 dir.txt
C:\Users\zhoufeiyue\Documents\sumdoc t411\(9+条消息)redis Jedis存取list对象和map - shenjianxz的博客 - CSDN博客.mh ...
- mysql删除唯一索引
在项目中用spring data jpa指定了一个唯一索引: @Entity @Table(name = "t_product") @Getter @Setter @AllArgs ...
- protobufjs@6.8.8 postinstall: `node scripts/postinstall`
由于Node.js 版本太低了, 使用最新版用 Node.js =================================== 以下解决方法来源于网络 npm ERR! Windows_NT ...