springboot使用gson解析返回数据
1.移除默认的jackson
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > < exclusions > <!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了 --> <!-- <exclusion>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-tomcat</artifactId>--> <!-- </exclusion>--> <!-- 去除默认的jackson --> < exclusion > < artifactId >jackson-core</ artifactId > < groupId >com.fasterxml.jackson.core</ groupId > </ exclusion > < exclusion > < artifactId >jackson-databind</ artifactId > < groupId >com.fasterxml.jackson.core</ groupId > </ exclusion > </ exclusions > </ dependency > |
2.添加gson依赖
1 2 3 4 5 6 | <!-- gson json解析工具类 --> < dependency > < groupId >com.google.code.gson</ groupId > < artifactId >gson</ artifactId > < version >2.8.2</ version > </ dependency > |
3.添加配置类GsonConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.zbbz.framework.config; import com.google.gson.*; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import springfox.documentation.spring.web.json.Json; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; @Configuration public class GsonConfig implements JsonSerializer< Json > { @Bean public HttpMessageConverters customConverters() { Collection< HttpMessageConverter <?>> messageConverters = new ArrayList<>(); GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(); gsonHttpMessageConverter.setGson( new GsonBuilder() // 将时间类型统一序列化为长整型,方便前后端通信 // .registerTypeAdapter(java.util.Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG) // 将时间类型统一反序列化为Date型,方便前后端通信 // .registerTypeAdapter(java.util.Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG) // 将长整型统一换成字符串 // .registerTypeAdapter(Long.class, new LongSerializer()) // 将整型统一换成字符串 // .registerTypeAdapter(Integer.class, new IntegerSerializer()) .setDateFormat("yyyy-MM-dd HH:mm:ss")//日期格式化 .registerTypeAdapter(Json.class, new GsonConfig()) .create()); messageConverters.add(gsonHttpMessageConverter); return new HttpMessageConverters(true, messageConverters); } @SuppressWarnings("deprecation") @Override public JsonElement serialize(Json json, Type type, JsonSerializationContext context) { final JsonParser parser = new JsonParser(); return parser.parse(json.value()); } } |
4.添加配置类GsonHttpMessageConverterConfiguration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.zbbz.framework.config; import com.google.gson.Gson; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.GsonHttpMessageConverter; @Configuration @ConditionalOnClass(Gson.class) @ConditionalOnMissingClass("com.fasterxml.jackson.core.JsonGenerator") @ConditionalOnBean(Gson.class) public class GsonHttpMessageConverterConfiguration { @Bean @ConditionalOnMissingBean public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) { GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); converter.setGson(gson); return converter; } } |
5.启动类中排除jackson
@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
分类:
springboot
标签:
springboot gson
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义