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

作者 : admin 本文共3382个字,预计阅读时间需要9分钟 发布时间: 2024-06-16 共1人阅读

目录

一、使用场景

二、环境准备

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的属性、忽略某些属性插图(1)

(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的属性、忽略某些属性插图(2)

(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的属性、忽略某些属性插图(3)

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

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

本站无任何商业行为
个人在线分享 » java后端返回数据给前端时去除值为空或NULL的属性、忽略某些属性
E-->