Jackson Annotations

From NovaOrdis Knowledge Base
Revision as of 22:35, 26 February 2017 by Ovidiu (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

@JsonProperty

Annotating a getter with @JsonProperty as follows will instruct the ObjectMapper to map the "name" property to "projectName" key in generated JSON content and vice-versa.

@JsonProperty("projectName")
public String getName() {
    return name;
}

Note that "{"projectName":"something"} comes on the wire and there is no annotation on the mutator setName(String s), Jackson is smart enough to figure out that @JsonProperty("projectName") on the corresponding accessor applies.

@JsonIgnoreProperties

Class-level annotation. Do not include the given keys into JSON:

@JsonIgnoreProperties({"someKey"})
public interface Something {
...
}

Note that both a base class and a subclass is annotated with @JsonIgnoreProperties, the base class annotation is ignored - it is overridden by the subclass'. That is why it is preferable to use [@JsonIgnore|Jackson#JsonIgnore] on individual properties.

@JsonIgnore

Property-level annotation.

@JsonInclude(JsonInclude.Include.NON_EMPTY)

If a getter is annotated with this, the null values won't be included as empty keys.

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public String getDescription() {
    return description;
}

@JsonCreator