具有@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";
}
如果我使用杰克逊序列化它,我将得到以下字符串:
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?
推荐答案
Jackson不会对@JsonIgnore注释的属性/方法进行序列化/反序列化.而未注释的将是.
The @JsonIgnore annotated properties/methods will not be serialized/deserialized by Jackson. While the not annotated will be.
这里的问题是,杰克逊通常会寻找吸气剂,而您没有指定任何吸气剂.这就是为什么它仅序列化@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屋!