背景:fastjson默認會過濾對象中屬性爲null值 html
問題:下面的配置沒有將對象屬性爲null的值過濾spring
<bean name="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="serializerFeatures">
<array>
<!--輸出key時是否使用雙引號,默認爲true-->
<value>QuoteFieldNames</value>
<!--全局修改日期格式-->
<value>WriteDateUseDateFormat</value>
<!--按照toString方式獲取對象字面值-->
<value>WriteNonStringValueAsString</value>
<!-- 字段若是爲null,輸出爲"",而非null -->
<value>WriteNullStringAsEmpty</value>
</array>
</property>
</bean>json
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<description>JSON轉換器</description>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig" ref="fastJsonConfig"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>後端
緣由: FastJsonConfig中配置了<value>WriteNullStringAsEmpty</value>屬性,會Object、Date類型值默認會將以null,類型輸出,字符串會展現成""springboot
影響:若是是作先後端分離,大量null值屬性,回傳到客戶端,浪費客戶端流量,也影響性能mvc
處理方式: FastJsonConfig中去除<value>WriteNullStringAsEmpty</value>屬性,部分你不須要顯示的屬性使用@JSONField(serialize = false)註解app
fastjson方式springboot方式配置前後端分離
@Configuration public class FastJsonConverterConfig { @Bean public HttpMessageConverters customConverters() { FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteNonStringValueAsString); FastJsonHttpMessageConverter fastJson = new FastJsonHttpMessageConverter(); fastJson.setFastJsonConfig(fastJsonConfig); List<MediaType> mediaTypeList = new ArrayList<>(); mediaTypeList.add(MediaType.valueOf("application/json;charset=UTF-8")); mediaTypeList.add(MediaType.valueOf("text/html;charset=UTF-8")); fastJson.setSupportedMediaTypes(mediaTypeList); return new HttpMessageConverters(fastJson); } }