Ignoring property when deserializing

Version 2.6.0+ allows this to be done with @JsonIgnoreProperties at the class level.

@JsonIgnoreProperties(value={ "money" }, allowGetters=true)

Take a look at this closed issue: https://github.com/FasterXML/jackson-databind/issues/95


In a case of you don't own or can't change the class by adding @JsonIgnore annotation, you would get expected result by using mixin starting from version 2.5 in your implementation.

public abstract class HasMoneyMixin {
    @JsonIgnore
    public abstract Money getMoney();
}

configure mapper to use mixin,

ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(HasMoney.class, HasMoneyMixin.class);
// avoid failing if all properties are empty
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

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:

public class UserAccount implements HasMoney {
    @JsonIgnore
    private BigDecimal money;

    // Other variable declarations, constructors

    @Override
    @JsonProperty
    public BigDecimal getMoney() {
        return money;
    }

    @JsonIgnore
    @Override
    public void setMoney(final BigDecimal money) {
        this.money = money;
    }

    // Other getters/setters
}

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.

Tags:

Java

Json

Jackson