java @JsonInclude(Include.NON_NULL) 没有按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26256720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@JsonInclude(Include.NON_NULL) not working as expected
提问by Ninad Pingale
I have added @JsonInclude(Include.NON_NULL)
annotation on Response class.
我@JsonInclude(Include.NON_NULL)
在 Response 类上添加了注释。
@JsonInclude(Include.NON_NULL)
public class Response {
@JsonProperty
private String message;
// getter-setters
}
If the value is null the property does not include in JSON
如果值为 null,则该属性不包含在 JSON 中
But still I am getting this property as a NULL.
但我仍然将此属性设为 NULL。
{
"message": null
}
What can be the reason ? Am I missing anything ?
可能是什么原因?我错过了什么吗?
采纳答案by Ninad Pingale
I tried
我试过
@JsonSerialize(include = Inclusion.NON_NULL)
intead of
而是
@JsonInclude(Include.NON_NULL)
and it worked as expected.
它按预期工作。
回答by Bosco Han
This has been deprecated:
这已被弃用:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
Use instead:
改用:
@JsonInclude(Include.NON_NULL)
回答by Arpan Jain
I am using Java Spring & in that, I am implementing like :
我正在使用 Java Spring & 在其中,我正在实现:
Class Level:
班级:
import com.fasterxml.Hymanson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Student {
String name;
//getter-setter
}
You can also implement it on elements level of class :
您还可以在 class 的元素级别上实现它:
Elements Level:
元素等级:
import com.fasterxml.Hymanson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Student {
String name;
@JsonInclude(JsonInclude.Include.NON_NULL)
String address;
//getter-setter
}