Jackson:JsonIdentityInfo 序列化深度只有一个 child

Jackson: JsonIdentityInfo serialises depth of only one child

出于某种原因,JsonIdentityInfo 序列化了一个 child 而不是另一个的深度。我的例子:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class A {
  private long id;
  private B last;

  // Getters, setters...
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class B {
  private long id;
  private A a;
  private C c1;
  private C c2;

  // Getters, setters...
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
class C {
  private long id;
  private Set<A> as;
  private B last;

  // Getters, setters...
}

我序列化 object B 它序列化 child A a 到一定深度,序列化 C c1 到几层深度。但是 C c2 只得到引用。

我希望 A a; C c1; C c2; 仅序列化到第一个深度,或者还包括 c2 而不管深度。

只需在 属性 c1 和 class B 中的 c2 上使用 @JsonUnwrapped 注释。即

@JsonIdentityInfo(generator = 
ObjectIdGenerators.IntSequenceGenerator.class, property = "id")

class B {
  private long id;
  private A a;
  @JsonUnwrapped
  private C c1;
  @JsonUnwrapped
  private C c2;

  // Getters, setters...
}