我有一个带有 @JsonUnwrapped 注释的字段的类:
const onPressDecrement = () => setCount((prevCount) => (Math.max(prevCount - 1,1)));
我需要动态更新该类中的其他字段(field1、2、...),因此我想为此使用 jackson update:
public static class MyClass {
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private Integer field2;
@JsonProperty("field3")
private SomeOtherClass field3;
@JsonUnwrapped
private NestedClass unwrapped;
}
问题是如果嵌套字段用@JsonUnwrapped 标记,Jackson 不会合并它们。
更新后 unwrapped 将变为 null - 有关详细信息,请参阅 here。
如果它们是普通的嵌套类字段,我可以使用@JsonMerge 或将映射器设置为默认合并。
我正在寻找仍然使用 jackson update 但不使用 @JsonUnwrapped 覆盖该字段的方法。
我可以在更新前手动获取 mapper.updateValue(myClass, patch);
字段实例,并在使用反射后将其设置回来。
但感觉很hacky,我正在寻找一种更干净的方式