Only using @JsonIgnore during serialization, but not deserialization
Exactly how to do this depends on the version of Jackson that you're using. This changed around version 1.9, before that, you could do this by adding @JsonIgnore
to the getter.
Which you've tried:
Add @JsonIgnore on the getter method only
Do this, and also add a specific @JsonProperty
annotation for your JSON "password" field name to the setter method for the password on your object.
More recent versions of Jackson have added READ_ONLY
and WRITE_ONLY
annotation arguments for JsonProperty
. So you could also do something like:
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)private String password;
Docs can be found here.
In order to accomplish this, all that we need is two annotations:
@JsonIgnore
@JsonProperty
Use @JsonIgnore
on the class member and its getter, and @JsonProperty
on its setter. A sample illustration would help to do this:
class User { // More fields here @JsonIgnore private String password; @JsonIgnore public String getPassword() { return password; } @JsonProperty public void setPassword(final String password) { this.password = password; }}
Since version 2.6: a more intuitive way is to use the com.fasterxml.jackson.annotation.JsonProperty
annotation on the field:
@JsonProperty(access = Access.WRITE_ONLY)private String myField;
Even if a getter exists, the field value is excluded from serialization.
JavaDoc says:
/** * Access setting that means that the property may only be written (set) * for deserialization, * but will not be read (get) on serialization, that is, the value of the property * is not included in serialization. */WRITE_ONLY
In case you need it the other way around, just use Access.READ_ONLY
.