fastJson 前端对象null转为空字符串 “ “,空数组 [ ],空对象{ }

Jackson请看 (@JsonProperty)
SpringBoot Jackson 将null转字符串"" ,List、Array转[],int转0

前提:使用FastJson的 @JsonField注解

import java.util.List;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 
@Configuration
@EnableWebMvc //覆盖springBoot的MVC配置
public class WebMvcConfigurer extends WebMvcConfigurationSupport{
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerialzerFeatures(
        		//List字段如果为null,输出为[]
        		SerializerFeature.WriteNullListAsEmpty,
        		//输出值为null的字段,默认为false
                SerializerFeature.WriteMapNullValue,
                //字符串null返回空字符串
                SerializerFeature.WriteNullStringAsEmpty,
                //空布尔值返回false
                SerializerFeature.WriteNullBooleanAsFalse,
                //结果是否格式化,默认为false
                SerializerFeature.PrettyFormat);  
                
        //格式化日期
        fastJsonConfig.setDateFormat("YYYY-MM-dd HH:mm:ss");
        convert.setFastJsonConfig(fastJsonConfig);
        converters.add(converter);
   }

SpringBoot2.0的新特性的问题,这个版本不支持WebMvcConfigurerAdpter ,新的实现有两种方法,如下:

一、
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
  //省略
}
二、
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
  //省略
}
推荐

补充 : 查看过时方法的替代,技巧如下,过时方法前的箭头即为替代方法
在这里插入图片描述

参考文章:
springboot返回前端对象null转为空字符串
fastjson数据格式转换(一) SerializerFeature属性详解
WebMvcConfigurerAdpter 已过时

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackHuan_code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值