有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@JsonIgnore行为

在处理项目时,我犯了一个错误,将@JsonIgnore放在setter而不是getter属性上,如下所示

private Set<Book> books;  


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

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

这应该导致我的getbooks api调用工作正常(从postman调用)。 然而,我意识到@JsonIgnore即使在setter上设置了@JsonIgnore,也会阻止序列化

这就是@JsonIgnore的正常工作方式吗?我认为仅在getter上设置@JsonIgnore可以防止序列化。在这里,即使我把它放在getter或setter上,它也会阻止序列化

谢谢你的建议。谢谢


共 (1) 个答案

  1. # 1 楼答案

    javadoc(重点是我的):

    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).