JSON是先後端數據交互最流行的格式。在目前的項目開發中,常見的JSON的轉換器有三種(json-lib因爲自身的缺陷基本不用,最普遍的仍是Jackson):java
在springboot中默認添加了jackson-databind
做爲JSON的處理器,不須要配置其餘處理器。若是開發者配置了MappingJackson2HttpMessageConverter
則使用開發者本身配置的,不然系統return new MappingJackson2HttpMessageConverter(objectMapper)
,源碼以下:web
@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);
}
}
}
複製代碼
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setJsonPrefix("aaa>>>");
return converter;
}
}
複製代碼
對於字段忽略和日期格式化等常見需求能夠使用@JsonIgnore
和@JsonFormat
解決。spring
springboot默認提供了Gson的自動轉化類GsonHttpMessageConverter
,添加依賴後與使用Jackson無異。 使用Gson須要去除默認的jackson-databind
並添加Gson依賴:json
<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());
}
}
複製代碼
不一樣於jackson以及gson,fastjson要求開發者必須手動配置HttpMessageConverter。首先排除jackson,而後加入gson依賴:tomcat
<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的配置springboot
@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;
}
}
複製代碼