Jackson Annotations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Jackson Full Data Binding")
 
No edit summary
 
Line 2: Line 2:


* [[Jackson_Full_Data_Binding#Annotations|Jackson Full Data Binding]]
* [[Jackson_Full_Data_Binding#Annotations|Jackson Full Data Binding]]
=Overview=
=@JsonProperty=
Annotating a getter with  <tt>@JsonProperty</tt> as follows will instruct the ObjectMapper to map the "name" property to "projectName" key in generated JSON content and vice-versa.
<pre>
@JsonProperty("projectName")
public String getName() {
    return name;
}
</pre>
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:
<pre>
@JsonIgnoreProperties({"someKey"})
public interface Something {
...
}
</pre>
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.
<pre>
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public String getDescription() {
    return description;
}
</pre>
=@JsonCreator=

Latest revision as of 22:35, 26 February 2017

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