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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM