Springboot2.x method to remove null fields when setting json response

If the server converts the object into json format and returns it to the front end, if it is not processed, the value of null will also be returned to the front end

SpringBoot solution:

1. If all java objects need to be filtered into JSON to filter null values, you can add a configuration in the application.yml configuration file:

spring:
  jackson:
    default-property-inclusion: non_null

If it is an individual java object that needs to deal with the problem of JSON filtering null values, it only needs to be configured on the Class or property

Configure on the class:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class User {
    private String userName;
    private String realName;
}

Configure on the properties:

public class User {
    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    private String userName;
    private String realName;
}

 

Guess you like

Origin blog.csdn.net/java_cxrs/article/details/104686641