JAXB Loop detected on object graph

I want to convert my pojo to json with JAXB, my pojo has a one-to-big relationship, and when I convert my pojo to json, JAXB generates the error "Loop detected in object graph. This will result in infinitely deep XML."

I read from the Internet that this problem can be solved with @XmlID and @XmlIDREF, but there is one problem, my attribute identifier is not string, but Long. and as far as I know, @XmlID can only be used with the String property.

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

+4
source share
3 answers

As you mentioned in your question, EclipseLink MOXy (I’m a Technology Lead) has @XmlInverseReference annotations to solve the bi-directional relationship problem. Starting with EclipseLink 2.4, MOXy can produce / consume JSON.

Additional Information

+3
source

You have a cyclic reference problem in your definition.

Try putting @XmlTransient over the problematic definition.

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

+1
source

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

One hack to use a full JAXB compatible implementation would be to duplicate your identifier in a String field (before serialization)

I don’t know much about JAXB, but XStream allows you to use different modes, and some of these modes will refer to the xpath address (absolute or relative) of the element in your xml if these elements are already displayed, (And you can do Json with XStream)

0
source

Source: https://habr.com/ru/post/1339011/


All Articles