JAXB A cycle is detected in the object graph

2019-01-26 13:02发布

I want to convert my pojo to json with JAXB, my pojo have one to many relation, and when i convert my pojo to json, JAXB generate error "A cycle is detected in the object graph. This will cause infinitely deep XML".

I read from web that, this problem can be solved with help from @XmlID and @XmlIDREF, but there is one problem, my Id attribute is not String type but Long. and as far as i know @XmlID can be used only with String property.

Other web suggest using eclipselink MOXy, but MOXy cannot generate json.

标签: json jaxb
3条回答
三岁会撩人
2楼-- · 2019-01-26 13:45

You have a cyclic reference problem in your definition.

Try putting @XmlTransient above the problematic definition.

Also, about XmlID and string type, see http://markmail.org/message/up6vrzjixxrvy5th.

查看更多
Fickle 薄情
3楼-- · 2019-01-26 13:50

The JAXB specification requires that the property marked with @XmlID be a String property. MOXy impl allows to use long.

One hack to keep using full JAXB compliant implementation would be to duplicate your id in a String field (before serialising)

Don't know so much about JAXB but XStream makes you able to use different modes and some of these modes will give references to the xpath address (absolute or relative) of an element in your xml, if these elements are already displayed. (And you can do Json with XStream)

查看更多
Animai°情兽
4楼-- · 2019-01-26 14:06

As you mentioned in your question EclipseLink MOXy (I'm the tech lead) has the @XmlInverseReference annotation to solve the problem of bidirectional relationships. As of EclipseLink 2.4 MOXy can produce/consume JSON.

For More Information

查看更多
登录 后发表回答