使用没有注释的@JsonIdentityInfo

我使用Jackson 2.2.3将POJO序列化为JSON。 然后我遇到了问题,我无法序列化递归结构……我通过使用@JsonIdentityInfo =>解决了这个问题。

但是,我不希望这个注释放在我的POJO之上。

所以我的问题是:是否还有其他可能设置我的ObjectMapper的默认行为来为每个POJO使用该function?

所以我想转换这个注释代码

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

喜欢的东西

 ObjectMapper om = new ObjectMapper(); om.setDefaultIdentityInfo(ObjectIdGenerators.IntSequenceGenerator.class, "@id"); 

有任何想法吗?

您可以使用Jackson混合注释或Jackson注释introspector来实现这一点。

以下是显示这两种方法的示例:

 public class JacksonJsonIdentityInfo { @JsonIdentityInfo( generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") static class Bean { public final String field; public Bean(final String field) {this.field = field;} } static class Bean2 { public final String field2; public Bean2(final String field2) {this.field2 = field2;} } @JsonIdentityInfo( generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id2") static interface Bean2MixIn { } static class Bean3 { public final String field3; public Bean3(final String field3) {this.field3 = field3;} } static class MyJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector { @Override public ObjectIdInfo findObjectIdInfo(final Annotated ann) { if (ann.getRawType() == Bean3.class) { return new ObjectIdInfo( PropertyName.construct("@id3", null), null, ObjectIdGenerators.IntSequenceGenerator.class, null); } return super.findObjectIdInfo(ann); } } public static void main(String[] args) throws JsonProcessingException { final Bean bean = new Bean("value"); final Bean2 bean2 = new Bean2("value2"); final Bean3 bean3 = new Bean3("value3"); final ObjectMapper mapper = new ObjectMapper(); mapper.addMixInAnnotations(Bean2.class, Bean2MixIn.class); mapper.setAnnotationIntrospector(new MyJacksonAnnotationIntrospector()); System.out.println(mapper.writeValueAsString(bean)); System.out.println(mapper.writeValueAsString(bean2)); System.out.println(mapper.writeValueAsString(bean3)); } } 

输出:

 {"@id":1,"field":"value"} {"@id2":1,"field2":"value2"} {"@id3":1,"field3":"value3"} 

经过几个月和大量的研究,我已经实现了自己的解决方案,以保持我的域清除jackson依赖。

 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); }