Skip to content
Advertisement

Ignoring property when deserializing

I have a simple interface with getter and setter for a property.

JavaScript

I have another class UserAccount which implements this interface.

JavaScript

My problem is that I want to serialize the money property but ignore while deserializing it i.e., dont accept any values from the user for this property. I have tried @JsonIgnore on setter and @JsonIgnore(false) on the getter, it does ignore it but it does so while serializing it also.

I tried @JsonIgnore on the setter and @JsonProperty on the getter just to explicitly tell Jackson that we intend to track this property, that seems to crash the application when money property is sent to the server and Jackson tries to deserialize it throwing up MalformedJsonException : cannot construct object of type Money.

The most wierd thing is that putting @JsonIgnore on the setter and @JsonProperty on the setter works for most cases when the property is primitive.

Advertisement

Answer

Ok, so the behavior of @JsonIgnore was radically changed from 1.9 onwards (for the worse imo). Without going into the devilish details of why your property is not being ignore during deserialization, try this code to fix it:

JavaScript

Note the use of @JsonIgnore on the field – its required for a working solution.

Note: depending on your environment and use case, you may need additional configuration on your ObjectMapper instance, for example, USE_GETTERS_AS_SETTERS, AUTO_DETECT_GETTERS, AUTO_DETECT_SETTERS etc.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement