gpt4 book ai didi

java - @JsonIgnore 行为

转载 作者:行者123 更新时间:2023-12-02 02:51:10 26 4
gpt4 key购买 nike

在处理项目时,我犯了一个错误,将 @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 上,它也会阻止序列化。

任何建议表示赞赏。谢谢。

最佳答案

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

关于java - @JsonIgnore 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com