幾個月後,有很多研究,我實現了自己的解決方案,以保持我的域名明確的傑克遜依賴。
public class Parent {
private Child child;
public Child getChild(){return child;}
public void setChild(Child child){this.child=child;}
}
public class Child {
private Parent parent;
public Child getParent(){return parent;}
public void setParent(Parent parent){this.parent=parent;}
}
首先,你必須聲明每個雙向關係的實體:
public interface BidirectionalDefinition {
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Parent.class)
public interface ParentDef{};
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Child.class)
public interface ChildDef{};
}
之後,對象映射器可以自動配置:
ObjectMapper om = new ObjectMapper();
Class<?>[] definitions = BidirectionalDefinition.class.getDeclaredClasses();
for (Class<?> definition : definitions) {
om.addMixInAnnotations(definition.getAnnotation(JsonIdentityInfo.class).scope(), definition);
}
喜,可能是你知道如何獲得沒有「@id」的輸出? 我的問題是在這裏http://stackoverflow.com/questions/33790565/jsonidentityinfo-how-to-not-serialize-id-property – serhii 2015-11-18 22:16:07
@Sloth,我想你可以從findObjectIdInfo方法int返回null他回答。我相信我理解你的用例。 – 2015-11-19 11:09:37