java - 在响应 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 文件中排除数据字段。
解决方案
这empty
在 JSON 响应中来自public
method isEmpty()
。将 也添加@JsonIgnore
到该公共方法以将其从 JSON 响应中删除。
推荐阅读
- scala - Scala — 有条件地替换数据框的列值
- ruby-on-rails - 使用 Carrierwave 进行 Rails 测试时出现 ArgumentError 异常
- javascript - 如何在 react/redux web 应用程序中持久化数据?
- ruby - 了解方法中错误的影响
- r - 重新排列不平衡的时间序列数据
- javascript - JavaScript localStorage 在页面加载后无法保存数据
- regex - 正则表达式查找带引号、分号和等于的值
- mysql - 当表的名称带有下划线时,PDO 在获取时返回 false
- c# - 构建干净的 Nuget2 存储库会出错
- tensorflow - Tensorflow 在多 GPU 训练中一次只有一个 GPU 处于活动状态