FastJsonHttpMessageConverter使用中问题

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

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

<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>

    <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,类型输出,字符串会展示成""

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

处理方式: FastJsonConfig中去除<value>WriteNullStringAsEmpty</value>属性,部分你不需要显示的属性使用@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<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);
    }
}

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

@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 创建一个FastJsonHttpMessageConverter实例,用于JSON数据的转换 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 创建并配置一个FastJsonConfig实例,用于定制JSON数据的处理规则 FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 设置JSON数据的序列化特性,此处为格式化输出,提高可读性 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 获取全局的序列化配置实例 SerializeConfig serializeConfig = SerializeConfig.globalInstance; // 将BigInteger、Long转为String类型 serializeConfig.put(BigInteger.class, ToStringSerializer.instance); serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); // 配置LocalDateTime类型的数据转换为字符串类型,以适应JSON格式 serializeConfig.put(LocalDateTime.class, ToStringSerializer.instance); // 将自定义的序列化配置应用到FastJsonConfig中 fastJsonConfig.setSerializeConfig(serializeConfig); // 将配置好的FastJsonConfig应用到FastJsonHttpMessageConverter中 fastConverter.setFastJsonConfig(fastJsonConfig); // 返回包含配置好的FastJsonHttpMessageConverter的HttpMessageConverters return new HttpMessageConverters(fastConverter); }为什么我不配置以上的消息转换 @PostMapping("test") public void test(@RequestBody Map deviceReported){ System.out.println("map = " + deviceReported); }这个接口调用会出现2025-03-29 18:07:30.897 WARN 17460 --- [ XNIO-1 task-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported] 2025-03-29 18:07:30.905 WARN 17460 --- [ XNIO-1 task-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]这个问题
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值