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 已过时