JSON是前後端資料互動最流行的格式。在目前的專案開發中,常見的JSON的轉換器有三種(json-lib由於自身的缺陷基本不用,最廣泛的還是Jackson):
Jackson
在springboot中預設新增了jackson-databind
作為JSON的處理器,不需要配置其他處理器。如果開發者配置了MappingJackson2HttpMessageConverter
則使用開發者自己配置的,否則系統return new MappingJackson2HttpMessageConverter(objectMapper)
,原始碼如下:
@Configuration
class JacksonHttpMessageConvertersConfiguration {
//中間程式碼省略
@Bean
@ConditionalOnMissingBean(
value = {MappingJackson2HttpMessageConverter.class},
ignoredType = {"org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter", "org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter"}
)
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
return new MappingJackson2HttpMessageConverter(objectMapper);
}
}
}
複製程式碼
Jackson自定義方式:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setJsonPrefix("aaa>>>");
return converter;
}
}
複製程式碼
對於欄位忽略和日期格式化等常見需求可以使用@JsonIgnore
和@JsonFormat
解決。
Gson
springboot預設提供了Gson的自動轉化類GsonHttpMessageConverter
,新增依賴後與使用Jackson無異。
使用Gson需要去除預設的jackson-databind
並新增Gson依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
複製程式碼
Gson自定義方式:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Bean
GsonHttpMessageConverter gsonHttpMessageConverter() {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd");
return new GsonHttpMessageConverter(builder.create());
}
}
複製程式碼
fastjson
不同於jackson以及gson,fastjson要求開發者必須手動配置HttpMessageConverter。首先排除jackson,然後加入gson依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjosn</artifactId>
<version>1.2.4</version>
</dependency>
複製程式碼
fastjson自定義方式:
手動配置convert。也可以重寫configureMessageConverters方法實現對HttpMessageConverter的配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy/MM/dd");
converter.setFastJsonConfig(config);
return converter;
}
}
複製程式碼