jackson反序列化JsonIdentityReference(alwaysAsId = true)
关注这个问题: 问题在这里
@JsonIdentityReference(alwaysAsId = true)
和@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)
在序列化结束时运行良好,但在反序列化时效果不佳,因为它无法解析对象ID引用。
有没有办法让它反序列化? 编写自定义反序列化器似乎有点矫枉过正。
您可以使用简单的setter反序列化器代替自定义反序列化器:
public class Container { @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @JsonIdentityReference(alwaysAsId = true) private Foo foo; public Foo getFoo() { return foo; } public Container setFoo(Foo foo) { this.foo = foo; return this; } @JsonProperty("foo") public void setFoo(String id) { foo = new Foo().setId(id); } }
在Jackson 2.5.2
使用此方法正确序列化了{"foo":"id1"}
示例字符串