• java后端返回数据给前端时去除值为空或NULL的属性、忽略某些属性


    目录

    一、使用场景

    二、环境准备

    1、引入依赖

    2、实体类

    三、示例

    1、不返回空值

    (1)方式

    (2)测试

    (3)说明

    2、不返回部分属性

    (1)方式

    (2)测试

    四、 Jackson常用注解

    1、 @JsonProperty

    2、@JsonPropertyOrder

    3、@JsonInclude

    4、@JsonIgnoreProperties

    5、@JsonFormat

    6、@JsonUnwrapped


    一、使用场景

            在开发过程中,有时候需要将后端数据返回前端,此时有些数据为空属性不需要返回,或者有些属性不需要返回,因此就需要处理。

    二、环境准备

    1、引入依赖

    1. <dependency>
    2. <groupId>com.fasterxml.jackson.coregroupId>
    3. <artifactId>jackson-coreartifactId>
    4. <version>2.10.0version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.fasterxml.jackson.coregroupId>
    8. <artifactId>jackson-annotationsartifactId>
    9. <version>2.10.0version>
    10. dependency>
    11. <dependency>
    12. <groupId>com.fasterxml.jackson.coregroupId>
    13. <artifactId>jackson-databindartifactId>
    14. <version>2.10.0version>
    15. dependency>

    2、实体类

    1. import lombok.AllArgsConstructor;
    2. import lombok.Data;
    3. import lombok.NoArgsConstructor;
    4. import java.math.BigDecimal;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. @Data
    8. @NoArgsConstructor
    9. @AllArgsConstructor
    10. public class Student{
    11. private Integer id;
    12. private String name;
    13. private Integer age;
    14. private String address;
    15. private BigDecimal score;
    16. private String className;
    17. private List subjectList = new ArrayList<>();
    18. }

    三、示例

    1、不返回空值

    (1)方式

    在实体类上面加上下面的注解:

    @JsonInclude(JsonInclude.Include.NON_EMPTY)

     

    (2)测试

    Controller里面的方法:

    1. @PostMapping("/getData")
    2. public R getData(){
    3. Student student = new Student();
    4. student.setName("Tom");
    5. student.setAge(22);
    6. return R.ok().data("student", student);
    7. }

    测试结果:

    (3)说明

    如果要对部分属性进行空值限制,分为两类:

    • 字符串、基本数据类型的设置,使用JsonInclude.Include.NON_NULL
    • 对象、数组之类的设置,使用JsonInclude.Include.NON_EMPTY
    1. import com.fasterxml.jackson.annotation.JsonInclude;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. import java.math.BigDecimal;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8. @Data
    9. @NoArgsConstructor
    10. @AllArgsConstructor
    11. public class Student{
    12. @JsonInclude(JsonInclude.Include.NON_NULL)
    13. private Integer id;
    14. @JsonInclude(JsonInclude.Include.NON_NULL)
    15. private String name;
    16. @JsonInclude(JsonInclude.Include.NON_NULL)
    17. private Integer age;
    18. @JsonInclude(JsonInclude.Include.NON_NULL)
    19. private String address;
    20. @JsonInclude(JsonInclude.Include.NON_NULL)
    21. private BigDecimal score;
    22. @JsonInclude(JsonInclude.Include.NON_NULL)
    23. private String className;
    24. @JsonInclude(JsonInclude.Include.NON_EMPTY)
    25. private List subjectList = new ArrayList<>();
    26. }

    2、不返回部分属性

    (1)方式

    实体类属性上使用注解:

    @JsonIgnore
    1. import com.fasterxml.jackson.annotation.JsonIgnore;
    2. import com.fasterxml.jackson.annotation.JsonInclude;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import lombok.NoArgsConstructor;
    6. import java.math.BigDecimal;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. @Data
    10. @NoArgsConstructor
    11. @AllArgsConstructor
    12. @JsonInclude(JsonInclude.Include.NON_EMPTY)
    13. public class Student {
    14. private Integer id;
    15. private String name;
    16. private Integer age;
    17. @JsonIgnore
    18. private String address;
    19. private BigDecimal score;
    20. private String className;
    21. private List subjectList = new ArrayList<>();
    22. }

    (2)测试

    Controller里面的方法:

    1. @PostMapping("/getData")
    2. public R getData(){
    3. Student student = new Student();
    4. student.setId(1001);
    5. student.setName("Tom");
    6. student.setAge(22);
    7. student.setAddress("浙江");
    8. return R.ok().data("student", student);
    9. }

    测试结果:

    四、 Jackson常用注解

    1、 @JsonProperty

            此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把testPwd属性序列化为pwd,@JsonProperty(value="pwd")。

    2、@JsonPropertyOrder

            作用在类上,被用来指明当序列化时需要对属性做排序,它有2个属性一个是alphabetic:布尔类型,表示是否采用字母拼音顺序排序,默认是为false,即不排序。如@JsonPropertyOrder(alphabetic=true)。

    3、@JsonInclude

            是用在实体类的方法类的头上 作用是实体类的参数查询到的为null的不显示,比如说你想传一些json数据到前台,但是不想传值为null的数据,就可以使用该标签。如@JsonInclude(JsonInclude.Include.NON_NULL)

    4、@JsonIgnoreProperties

            可以注明是想要忽略的属性列表如@JsonIgnoreProperties({"name","age","title"}),也可以注明过滤掉未知的属性如@JsonIgnoreProperties(ignoreUnknown=true),@JsonIgnore表示忽略当前属性。

    5、@JsonFormat

            用在属性和方法上,可以方便的进行格式转换,如把Date转换为我们要的模式@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。

    6、@JsonUnwrapped

            当实体类中成员属性是一个类的对象时候,忽略包装。直接显示属性。

  • 相关阅读:
    ICV:《中美量子产业融资比较分析》
    vue3 利用 Composition provide 实现祖先组件向后代组件传值
    Acwing 算法基础课 c++模板整理(附python语法基础题)
    使用Harbor搭建Docker仓库
    golang知识点整理
    基于自编译的onlyoffice镜像,关于修改字体的问题
    线性代数 --- 四个基本子空间(个人学习笔记)
    Nginx核心指标优化
    DevOps infra | 互联网、软件公司基础设施建设(基建)哪家强?
    Docker Hub 国内镜像源配置
  • 原文地址:https://blog.csdn.net/weixin_47382783/article/details/134093103