SpringBoot阿里巴巴Fastjson的一些常用配置

SpringBoot阿里巴巴Fastjson的一些常用配置

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,   //是否格式化输出,默认为false
                SerializerFeature.DisableCircularReferenceDetect,   //禁止循环引用,即出现$.data[0]
                SerializerFeature.WriteMapNullValue,   //Map字段如果为null,输出为[],而非null
                SerializerFeature.WriteNullListAsEmpty,   //List字段如果为null,输出为[],而非null
                SerializerFeature.WriteNullStringAsEmpty   //字符类型字段如果为null,输出为”“,而非null
        );
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");   //统一配置日期格式
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }

当配置了“SerializerFeature.WriteMapNullValue”这一类配置之后,当返回值中的该字段为null,将输出字段,如果不配置,那该字段将不会显示

引用文章:https://blog.csdn.net/u010246789/article/details/52539576

posted @ 2018-09-28 16:13  渡劫锦官城  阅读(868)  评论(0编辑  收藏  举报