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

springboot传参合和反参时值为null的处理

@JsonIgnoreProperties(ignoreUnknown = true)   //忽略传参时其他无用字段
@JsonInclude(JsonInclude.Include.NON_NULL)   // 忽略反参时值为null的字段
public class BaseAreaBO implements Serializable {
    @ApiModelProperty(value = "地区code")
    private String areaCode;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。