作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用我自己的 Java 实体类和 Jackson 注释生成 JSON 响应。两个键值必须是 null
在我的 JSON 响应中,但如果我将它们设置为 null
它们从 JSON 中消失。不幸的是,我仅限于 Jackson 1.9.13 版。
我已经尝试将值设置为 null
, 使用
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
我的响应实体:
public class Response {
@JsonProperty("data")
private Data data;
@JsonProperty("error")
private Error error;
public Data getData() {
return data;
}
public void setData(PgSoftAuthData data) {
this.data = data;
}
public Error getError() {
return error;
}
public void setError(PgSoftError error) {
this.error = error;
}
}
我正在尝试生成这样的响应:
Data data = new Data();
data.setName("Name");
data.setTime(null); // This key dissappear from JSON
Response responseSuccess = Response();
responseSuccess.setData(data);
responseSuccess.setError(null); // Error object disappear from JSON
我想得到以下回复:
{
"data" : {
"name" : "Name",
"time" : null
},
"error" : null
}
最佳答案
添加 JsonInclude到相关类(class) Response
和 Data
(使用@ThomasFritsch 评论更新)
@JsonInclude(JsonInclude.Include.ALWAYS)
Value that indicates that property is to be always included, independent of value of the property.
关于java - Jackson:如何在 JSON 序列化中包含空值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180616/
我是一名优秀的程序员,十分优秀!