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

06-01 1055阅读

目录

一、使用场景

二、环境准备

1、引入依赖

2、实体类

三、示例

1、不返回空值

(1)方式

(2)测试

(3)说明

2、不返回部分属性

(1)方式

(2)测试

四、 Jackson常用注解

1、 @JsonProperty

2、@JsonPropertyOrder

3、@JsonInclude

4、@JsonIgnoreProperties

5、@JsonFormat

6、@JsonUnwrapped


一、使用场景

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

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

二、环境准备

1、引入依赖

		
            com.fasterxml.jackson.core
            jackson-core
            2.10.0
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.10.0
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.10.0
        

2、实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student{
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    private BigDecimal score;
    private String className;
    private List subjectList = new ArrayList();
    
}

三、示例

1、不返回空值

(1)方式

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

@JsonInclude(JsonInclude.Include.NON_EMPTY)

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

(2)测试

Controller里面的方法:

    @PostMapping("/getData")
    public R getData(){
        Student student = new Student();
        student.setName("Tom");
        student.setAge(22);
        return R.ok().data("student", student);
    }

测试结果:

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

(3)说明

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

  • 字符串、基本数据类型的设置,使用JsonInclude.Include.NON_NULL
  • 对象、数组之类的设置,使用JsonInclude.Include.NON_EMPTY
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Student{
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private Integer id;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String name;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private Integer age;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String address;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private BigDecimal score;
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String className;
        @JsonInclude(JsonInclude.Include.NON_EMPTY)
        private List subjectList = new ArrayList();
    }
    

    2、不返回部分属性

    (1)方式

    实体类属性上使用注解:

    @JsonIgnore
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    public class Student {
        private Integer id;
        private String name;
        private Integer age;
        @JsonIgnore
        private String address;
        private BigDecimal score;
        private String className;
        private List subjectList = new ArrayList();
    }
    

    (2)测试

    Controller里面的方法:

        @PostMapping("/getData")
        public R getData(){
            Student student = new Student();
            student.setId(1001);
            student.setName("Tom");
            student.setAge(22);
            student.setAddress("浙江");
            return R.ok().data("student", student);
        }

    测试结果:

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

    四、 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

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

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码