SpringBoot返回结果null或空值 解决SpringBoot返回结果如果为null或空值不显示处理问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot返回结果null或空值 解决SpringBoot返回结果如果为null或空值不显示处理问题

孤独二世   2021-07-28 我要评论
想了解解决SpringBoot返回结果如果为null或空值不显示处理问题的相关内容吗,孤独二世在本文为您仔细讲解SpringBoot返回结果null或空值的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

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

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们