JSON is the most popular format for back-end data interaction. In the current project development, there are three common JSON converters (JSON-lib is rarely used due to its own defects, and Jackson is the most widely used) :

Jackson

Jackson-databind is added as the JSON handler by default in SpringBoot, and no additional processors need to be configured. If developer configuration MappingJackson2HttpMessageConverter is use their own configuration, Otherwise the system return new MappingJackson2HttpMessageConverter (objectMapper), the source code is as follows:

@Configuration
class JacksonHttpMessageConvertersConfiguration {
        // Middle code is omitted
        @Bean
        @ConditionalOnMissingBean(
            value = {MappingJackson2HttpMessageConverter.class},
            ignoredType = {"org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter"."org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter"})public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
            return newMappingJackson2HttpMessageConverter(objectMapper); }}}Copy the code

Jackson custom mode:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(a) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setJsonPrefix("aaa>>>");
        returnconverter; }}Copy the code

Common requirements such as field ignoring and date formatting can be addressed using @jsonIgnore and @jsonFormat.


Gson

Springboot default provides GsonHttpMessageConverter Gson automatically into class, add after rely on with Jackson. To use Gson, remove the default jackson-databind and add Gson dependencies:

<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>Copy the code

Gson custom mode:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(a) {
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd");
        return newGsonHttpMessageConverter(builder.create()); }}Copy the code

fastjson

Unlike Jackson and Gson, FastJSON requires developers to manually configure HttpMessageConverter. First exclude Jackson, then add gson dependencies:

<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. < / version > < / dependency >Copy the code

Fastjson Custom mode: Manually configure convert. Can also be rewritten configureMessageConverters method implementation of HttpMessageConverter configuration

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter(a) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy/MM/dd");
        converter.setFastJsonConfig(config);
        returnconverter; }}Copy the code