Spring Boot自定义Fastjson配置

某些情况下Fastjson的默认配置并不能满足业务需求,比如需要返回JSON时日期字符串格式话、null类型处理、Long类型处理等。

Spring Boot 1.x配置模板

@Configuration
public class WebMvcByFastjson extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        /**
         * 1、定义一个convert转换消息对象;
         * 2、添加fastJson的配置信息,是否要格式化返回的Json数据;
         * 3、处理中文乱码
         * 4、在convert中添加配置信息;
         * 5、将convert添加到converters当中;
         */
        // 1、定义一个convert转换消息对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        // 2、添加fastJson的配置信息,是否要格式化返回的Json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
            if (source instanceof LocalDateTime) {
                return DateUtil.dateTimeAsString((LocalDateTime)source,"yyyy-MM-dd HH:mm:ss");
            }
            return source;
        });
        fastJsonConfig.setSerializerFeatures(
                //浏览器兼容模式、Long类型返回字符串,否则17位以上的Long在js中会精度丢失
                SerializerFeature.BrowserCompatible,
                //null值返回""
                SerializerFeature.WriteNullStringAsEmpty
        );

        // 3、处理中文乱码
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        // 4、在convert中添加配置信息;
        fastConverter.setFastJsonConfig(fastJsonConfig);

        // 5、将convert添加到converters当中;
        converters.add(fastConverter);
    }

Spring Boot 2.x配置模板

@Configuration
public class WebMvcByFastjson implements WebMvcConfigurer {

    @Bean
    public HttpMessageConverter<Object> fastJsonHttpMessageConverter() {
        // 1.需要先定义一个convert 转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        // 2.添加fastJson的配置信息,比如,是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
            if (source instanceof LocalDateTime) {
                return DateUtil.dateTimeAsString((LocalDateTime)source,"yyyy-MM-dd HH:mm:ss");
            }
            return source;
        });
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.BrowserCompatible,
                SerializerFeature.WriteNullStringAsEmpty
        );

        // 处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);

        // 3.在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return fastConverter;
    }

    // 配置消息转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverter());

    }

}