What is the difference between a property with @JsonIgnore and one with no annotation?

走远了吗. 提交于 2020-02-03 05:33:05

问题


Consider the following class:

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"}

What is the difference between a property with @JsonIgnore and one with no annotation?


回答1:


The @JsonIgnore annotated properties/methods will not be serialized/deserialized by Jackson. While the not annotated will be.

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.

If you implement the 3 getters for the 3 properties, your json will look like this:

{
  "id":"ID",
  "noAnnotation":"NO_ANNOTATION"
}


来源:https://stackoverflow.com/questions/38077728/what-is-the-difference-between-a-property-with-jsonignore-and-one-with-no-annot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!