0
votes

While working on a project, I made the mistake of putting a @JsonIgnore on the setter rather than the getter property as below

private Set<Book> books;  


public Set<Book> getBooks() {
    return books;
}

@JsonIgnore
public void setBooks(Set<Book> books) {
    this.books = books;
}

This should have resulted in my getbooks api call working fine (calling from postman). However, i realise that @JsonIgnore prevents serialisation even if the @JsonIgnore is set on the setter.

Is this how normally @JsonIgnore works ? I thought setting @JsonIgnore only on the getter prevents serialisation. Here it is preventing serialisation even if i put it on the getter or the setter.

Any advice appreciated. Thanks.

1
have you referred this post on @JsonIgnore - Rajith Pemabandu
Thanks Ranjith. I happened to read that just before posting, but the message was nuanced and it probably didn't register in my thick skull. Re-reading that after RC's response below makes it all very clear now. Thanks. - HopeKing

1 Answers

2
votes

From the javadoc (emphasis is mine):

In addition, starting with Jackson 1.9, if this is the only annotation associated with a property, it will also cause cause the whole property to be ignored: that is, if setter has this annotation and getter has no annotations, getter is also effectively ignored. It is still possible for different accessors to use different annotations; so if only "getter" is to be ignored, other accessors (setter or field) would need explicit annotation to prevent ignoral (usually JsonProperty).