How does the json string returned by the springboot interface not display the null value field

How does the json string returned by the springboot interface not display the null value field

When POSTMAN tests the interface, the default field value is displayed even if it is null. How to remove it more concisely? This has nothing to do with POSTMAN, POSTMAN just shows the body of the response

Thinking : Why do you want to remove the fields with null values? Is it possible to reduce the amount of data sent when returning the value to the front end to save bandwidth?
(Domestic companies do not seem to pay much attention to this, and almost do not configure the null value field to not be displayed)

I think the display is also beneficial , that is, it is very clear and "explicitly" tells you that the value of this field is null, not that there is no such field in the returned structure.

In the same way, when printing logs , there will also be a dilemma. When printing the json of an object, should the null value field be printed? If you print a large amount of logs, if you don’t print it, you don’t know whether it’s because the field doesn’t exist in the object or just because the value of the field is null.

In the interface of springboot, the default null value field will also appear in the returned result (the entire field will not be hidden because it is a null value)

@GetMapping("/stu")
public Stu f() {
    
    
  Stu stu = new Stu();
  stu.setName("stone");
  stu.setAge(null);
  return stu;
}

return, age is still displayed

{
    
    
    "name": "stone",
    "age": null
}

insert image description here

Add the following in the properties configuration file, such as application.properties, and it will take effect globally for any return

spring.jackson.default-property-inclusion=non_null

If you only want to implement "null value field does not appear" for a certain class , use the@JsonInclude(JsonInclude.Include.NON_NULL)

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Stu {
    
    
    private String name;
    private Integer age;
}

Guess you like

Origin blog.csdn.net/w8y56f/article/details/130910331