springboot使用gson解析返回数据
1.移除默认的jackson
<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依赖
<!-- gson json解析工具类 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
3.添加配置类GsonConfig
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
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})