阿里的fastjson 包升级后,可能导致返回的json 数据,字段为null时不显示等问题
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.9.13</version> </dependency>
@Configuration public class FastjsonConfiguration { @Bean public HttpMessageConverters fastjsonConverter() { FastJsonConfig fastJsonConfig = new FastJsonConfig(); //自定义格式化输出 fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero); FastJsonHttpMessageConverter4 fastjson = new FastJsonHttpMessageConverter4(); fastjson.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastjson); } }
SerializerFeature 类的一些枚举值
WriteMapNullValue, WriteNullListAsEmpty // 将list的null转成 [] WriteNullStringAsEmpty // 将string的null转成 "" WriteNullNumberAsZero WriteNullBooleanAsFalse, PrettyFormat, WriteClassName, WriteDateUseDateFormat, NotWriteRootClassName, BeanToArray, WriteNonStringKeyAsString, NotWriteDefaultValue, BrowserSecure, IgnoreNonFieldGetter, WriteNonStringValueAsString,
springMVC 中设置
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</list>
</property>
<!-- <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/> -->
</bean>
2、脱敏设置
1 ) 自定义 SensentiveField 类 实现 ObjectSerializer,并重写其方法
2) @JSONField(serializeUsing =SensentiveField.class) 实体类的属性加上 注解