我有一个带有一个可选字段的对象,并且找不到适当的注释来建模它。任何用Jackson正确处理它的想法吗?
来晚了...
使用Spring HttpMessageConverter 4.3.6通过Jackson 2.8.6,我不得不将我的setter参数更改为未包装的类型,就像这样:
class Foo {
private Optional<Bar> bar;
public void setBar(Bar bar) { // NOT Optional<Bar>, this gives me Optional.empty()
this.bar = Optional.of(bar);
}
// getter doesn't need to be changed
}
我们正在使用Spring Boot 2.7.9和Jackson 2.13.5。
为了使属性在Jackson将输入JSON绑定到我们的DTO时变成可选的,我们在DTO类的顶部使用以下代码:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnore