FastJsonHttpMessageConverter使用中问题

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

背景:fastjson默认会过滤对象中属性为null值   

问题:下面的配置没有将对象属性为null的值过滤


        
            
                
                QuoteFieldNames
                
                WriteDateUseDateFormat
                
                WriteNonStringValueAsString
                
                WriteNullStringAsEmpty
            

        

    

    
        
            
                JSON转换器
                
                    
                        application/json;charset=UTF-8
                        text/html;charset=UTF-8
                    

                

                
            

        

    

原因:  FastJsonConfig中配置了WriteNullStringAsEmpty属性,会Object、Date类型值默认会将以null,类型输出,字符串会展示成""

影响:如果是做前后端分离,大量null值属性,回传到客户端,浪费客户端流量,也影响性能

处理方式: FastJsonConfig中去除WriteNullStringAsEmpty属性,部分你不需要显示的属性使用@JSONField(serialize = false)注解

 

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 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);
    }
}

转载于:https://my.oschina.net/u/1017791/blog/2874132

你可能感兴趣的:(FastJsonHttpMessageConverter使用中问题)