JAXB A cycle is detected in the object graph

后端 未结 4 800
说谎
说谎 2020-12-19 11:59

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 g

相关标签:
4条回答
  • 2020-12-19 12:24

    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.

    0 讨论(0)
  • 2020-12-19 12:27

    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

    • http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
    • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
    0 讨论(0)
  • 2020-12-19 12:27

    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)

    0 讨论(0)
  • 2020-12-19 12:37

    I faced similar problem when I wanted to convert my POJO to JSON with JaxRS. The MoxyJsonProvider is the default option of eclipselink but it fails to parse the JSOG (where cycles exists in the JSON structure). Jackson Jaxb Provider does this better with ObjectMapper.

    I have elaborated in this answer below, about how to invoke Jackson Provider instead of Moxy. You will need jackson packages in your pom xml.

    https://stackoverflow.com/a/60319306/5076414

    0 讨论(0)
提交回复
热议问题