When the java backend returns data to the front end, attributes with empty or NULL values are removed and certain attributes are ignored.

Table of Contents

1. Usage scenarios

2. Environmental preparation

1. Introduce dependencies

2. Entity class

3. Example

1. Do not return null value

(1)Method

(2)Test

(3)Explanation

2. Do not return some attributes

(1)Method

(2)Test

4. Jackson common annotations

1. @JsonProperty

2. @JsonPropertyOrder

3. @JsonInclude

4. @JsonIgnoreProperties

5. @JsonFormat

6. @JsonUnwrapped


1. Usage scenarios

During the development process, sometimes it is necessary to return back-end data to the front-end. At this time, some data have empty attributes and do not need to be returned, or some attributes do not need to be returned, so they need to be processed.

2. Environment preparation

1. Introduce dependencies

 <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
        </dependency>

2. Entity class

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<String> subjectList = new ArrayList<>();

    
}

3. Example

1. Do not return null value

(1)method

Add the following annotation to the entity class:

@JsonInclude(JsonInclude.Include.NON_EMPTY)

(2)Test

Methods in Controller:

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

Test Results:

(3)Description

If you want to restrict null values for some attributes, it can be divided into two categories:

  • For string and basic data type settings, use JsonInclude.Include.NON_NULL
  • For settings such as objects and arrays, use 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<String> subjectList = new ArrayList<>();

}

2. Do not return some attributes

(1) method

Use annotations on entity class attributes:

@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<String> subjectList = new ArrayList<>();

}

(2)Test

Methods in Controller:

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

Test Results:

4. Jackson common annotations

1, @JsonProperty

This annotation is used on attributes, and its function is to serialize the name of the attribute to another name, such as serializing the testPwd attribute to pwd, @JsonProperty(value=”pwd”).

2, @JsonPropertyOrder

Acts on the class and is used to indicate that the attributes need to be sorted when serializing. It has two attributes. One is alphabetic: Boolean type, indicating whether to sort in alphabetical and pinyin order. The default is false, that is, no sorting. Such as @JsonPropertyOrder(alphabetic=true).

3, @JsonInclude

It is used on the header of the method class of the entity class. The function of the parameter query of the entity class is that it is null and is not displayed. For example, if you want to pass some json data to the front desk, but do not want to pass the data with null value, you can use this Label. Such as @JsonInclude(JsonInclude.Include.NON_NULL)

4, @JsonIgnoreProperties

You can indicate the list of properties you want to ignore, such as @JsonIgnoreProperties({“name”, “age”, “title”}), or you can indicate the filtering out unknown properties, such as @JsonIgnoreProperties(ignoreUnknown=true), @JsonIgnore means Ignore current properties.

5, @JsonFormat

Used on properties and methods, it can be used for convenient format conversion, such as converting Date into the pattern we want @JsonFormat(pattern = “yyyy-MM-dd HH-mm-ss”).

6, @JsonUnwrapped

When a member attribute in an entity class is an object of a class, packaging is ignored. Display properties directly.