8

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?

mattalxndr
  • 9,143
  • 8
  • 56
  • 87

1 Answers1

8

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"
}
Renato Souza
  • 123
  • 5
  • Then what's the difference between a field with no annotations and a field with @JsonProperty? – Frax Oct 31 '18 at 07:51
  • 2
    When there is no @JsonProperty, the generated output would be based on the property name. Let's say that your property was named textDescription, but you need it as "text-description". Then you should use the @JsonProperty("text-description") – Renato Souza Jun 25 '19 at 21:59