I have a user object that is sent to and from the server. When I send out the user object, I don’t want to send the hashed password to the client. So, I added @JsonIgnore on the password property, but this also blocks it from being deserialized into the password that makes it hard to sign up users when they don’t have a password.
How can I only get @JsonIgnore to apply to serialization and not deserialization? I’m using Spring JSONView, so I don’t have a ton of control over the ObjectMapper.
Things I’ve tried:
- Add
@JsonIgnoreto the property - Add
@JsonIgnoreon the getter method only
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
@JsonIgnoreto the getter.Which you’ve tried:
Do this, and also add a specific
@JsonPropertyannotation for your JSON "password" field name to the setter method for the password on your object.More recent versions of Jackson have added
READ_ONLYandWRITE_ONLYannotation arguments forJsonProperty. So you could also do something like:Docs can be found here.