Spring Boot之自定义JSON转换器

5,525 阅读2分钟

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;
    }
}