5

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.

artbristol
  • 32,010
  • 5
  • 70
  • 103
nathan21
  • 65
  • 1
  • 3
  • As of EclipseLink 2.4 MOXy has native support for JSON-binding: http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html – bdoughan Apr 02 '12 at 18:41

4 Answers4

3

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks u Mr Blaise for the solution, and Mr. Rodrigo Hahn, Mr Sebastien Lorber thank's for the suggest – nathan21 Feb 11 '11 at 03:16
  • @Mr Blaise I see somewhere in web, that's MOXy cannot run in glassfish environtment? is this true? – nathan21 Feb 11 '11 at 08:11
  • EclipseLink MOXy certainly runs in the GlassFish environment (it is my standard demo platform), for setup instructions see: http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html – bdoughan Feb 15 '11 at 21:52
  • what is the equal of `@XmlInverseReference` in Jersy? – William Kinaan May 22 '13 at 13:22
  • You can use MOXy with Jersey for JSON-binding http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html and https://github.com/jersey/jersey/blob/master/examples/json-moxy – bdoughan May 22 '13 at 13:27
1

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

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)

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
0

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

Sacky San
  • 1,535
  • 21
  • 26