在响应json文件中排除数据成员



我在Json响应文件中得到不需要的数据字段。我尝试使用@JsonIgnore,但仍然在输出中得到不需要的字段。下面是模型类:

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Data
@JsonInclude(Include.NON_NULL)
public class RToleranceResponse implements Serializable  {

//This object is used to check if the object is empty or not (all data members are null)
@JsonIgnore
private static final RToleranceResponse EMPTY = new RToleranceResponse();

private static final long serialVersionUID = -8284498145392258180L;
private String description;
private Double totalMean;
private Double Rmrisk;
private Double upsideHm;

// Function to check if the current object is empty
public boolean isEmpty() {
return this.equals(EMPTY);
}
}

在Postman上的Json输出:

"tolerance": {
"description": "Very conservative",
"empty": false
}

不希望这个EMPTY变量存在。其中EMPTY变量用于检查整个对象是否为空(正如我使用new关键字一样)。这只是一个示例类和输出,以保持问题的小。那么如何从响应Json文件中排除数据字段呢?

这个JSON响应中的empty来自public方法isEmpty()。将@JsonIgnore添加到该公共方法中,也可以将其从JSON响应中删除。