带有 @JsonIgnore 的属性和没有注释的属性有什么区别? [英] What is the difference between a property with @JsonIgnore and one with no annotation?
问题描述
考虑以下类:
private static class Widget {
@JsonProperty
private String id = "ID";
@JsonIgnore
private String jsonIgnored = "JSON_IGNORED";
private String noAnnotation = "NO_ANNOTATION";
}
如果我使用 Jackson 对其进行序列化,我最终会得到这个字符串:
If I serialize this using Jackson, I will end up with this string:
{"id":"ID"}
带有 @JsonIgnore
的属性和没有注释的属性有什么区别?
What is the difference between a property with @JsonIgnore
and one with no annotation?
推荐答案
@JsonIgnore 注释的属性/方法不会被 Jackson 序列化/反序列化.而未注释的将是.
The @JsonIgnore annotated properties/methods will not be serialized/deserialized by Jackson. While the not annotated will be.
这里的问题是 Jackson 通常会查找 getter,而您没有指定任何 getter.所以这就是为什么它只序列化了@JsonProperty 注释的属性.
The problem here is that Jackson usually looks for the getters, and you didn't specified any getters. So that's why it's only serialized the @JsonProperty annotated property.
如果您为 3 个属性实现 3 个 getter,您的 json 将如下所示:
If you implement the 3 getters for the 3 properties, your json will look like this:
{
"id":"ID",
"noAnnotation":"NO_ANNOTATION"
}
这篇关于带有 @JsonIgnore 的属性和没有注释的属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!