gpt4 book ai didi

java - @JsonIgnoreProperties 用于已知和未知属性

转载 作者:太空狗 更新时间:2023-10-29 22:44:25 25 4
gpt4 key购买 nike

我当前忽略 JPA 实体已知属性的注释是:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","created","updated","createdBy","lastUpdatedBy"})

除了忽略这些类属性外,我还想忽略服务器接收到的任何未知属性。我知道通过以下注释忽略未知属性的唯一方法:

@JsonIgnoreProperties(ignoreUnknown=true)

但不确定如何将其添加到我上面给出的当前注释中。我在下面尝试了多种方法,但似乎都没有用,而且我在网上找不到这种情况的示例。

文档中的任何示例或线索也会有所帮助。

最佳答案

设置ignoreUnknowntrue 并在 value 中定义要忽略的属性名称元素:

@JsonIgnoreProperties(ignoreUnknown = true, 
value = {"hibernateLazyInitializer", "handler", "created"})

它是如何工作的?

看看 documentation 中的这句话(突出显示是我的):

In its simplest form, an annotation looks like the following:

@Entity

The at sign character (@) indicates to the compiler that what follows is an annotation. In the following example, the annotation's name is Override:

@Override
void mySuperMethod() { ... }

The annotation can include elements, which can be named or unnamed, and there are values for those elements:

@Author(name = "Benjamin Franklin", date = "3/27/2003")
class MyClass() { ... }

or

@SuppressWarnings(value = "unchecked")
void myMethod() { ... }

If there is just one element named value, then the name can be omitted, as in:

@SuppressWarnings("unchecked")
void myMethod() { ... }

处理未知属性的其他方法

要忽略未知属性,您还可以这样做:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

关于java - @JsonIgnoreProperties 用于已知和未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317816/

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