gpt4 book ai didi

java - 对于接受多个参数的 setter,是否有类似于 @JsonCreator 的注释?

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:44 24 4
gpt4 key购买 nike

我有一个包含很多字段的可序列化对象,其中一些是强制性的,其他是可选的。为了使这个对象易于使用,我尝试了 Builder Pattern with a Twist 。这很有效,但是生成的某些 Builder 方法需要多个参数。这是一个例子:

@JsonProperty("foo")
public Builder setFoo(String foo) {
this.foo = foo;
return this;
}

public Builder setBarAndZip(@JsonProperty("bar") String bar, @JsonProperty("zip") String zip) {
this.bar = bar;
this.zip = zip;
return this;
}

假设我用 @JsonDeserialize(builder = MyClass.Builder.class) 注释我的父类(此生成器创建的类) , jackson handle foo没有问题。但是,它不会调用 setBarAndZip(...) ,因此 bar 都没有或zip进行反序列化。

我发现我可以通过输入我的 @JsonProperty 来解决这个问题直接在构建器的私有(private)字段上注释,而不是在 setter 上,所以我有一个解决方法,但是有更好的方法吗?

构造函数可以在 @JsonCreator 的帮助下接受多个参数注解。是否有一个等效的注释可以应用于 setBarAndZip(...)这会让 jackson 看到@JsonProperty其参数的注释?

最佳答案

来自jackson-user Google Groups页面:

Tatu Saloranta

No, currently there is no way to have what amounts to multiple-argument setters. From annotation perspective it should be possible to just use @JsonSetter for such methods, but internally those are not yet supported. They would be nice to support, but would likely require lots of work due to internal assumptions that each setter method only handles single property.

-+ Tatu +-

关于java - 对于接受多个参数的 setter,是否有类似于 @JsonCreator 的注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752809/

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