SpringBoot接口返回结果为null或空值不显示处理方法

SpringBoot返回结果为null或空值不显示处理方法

 

 

方法一:自定义消息转换器

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
    /**
     * 利用fastjson替换掉jackson
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    }
}

 

方法二:在application.yml配置文件中

spring:
  jackson:
    default-property-inclusion: non_null

方法三:单DTO空值过滤,DTO上添加如下注解

@JsonInclude(JsonInclude.Include.NON_EMPTY)    
@JsonInclude(JsonInclude.Include.NON_NULL)

 

posted @ 2020-12-31 00:12  panchanggui  阅读(2205)  评论(0编辑  收藏  举报