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

时间:2023-03-08 21:39:11

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.****.net/u010246789/article/details/52539576