File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes jaxb & object graphs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "jaxb & object graphs" Watch "jaxb & object graphs" New topic

jaxb & object graphs

Antero Laukkanen

Joined: Aug 24, 2007
Posts: 6
Hi ya!

I need to get an object graph into xml and back to object again. Get into trouble with cycles. The problem is very clear.

I have an object referencing a node in a tree structure. That nodes holds references to both parent and subtasks. If we always started to advance from the root I understand it would be straightforward to use the afterUnmarshall(Unmarshaller, Object parent) callback to set the parent pointer for the node after the object has been unmarshalled. But not so when we need to traverse the tree starting from a node in the middle of the tree..?? Or should this be a piece of cake also? Anyone wrestled a similar problem?
Peer Reynders

Joined: Aug 19, 2005
Posts: 2933
Solution to your problem: Stop trying to send object graphs over web services.

Despite popular opinion web services exchange messages - not objects or object graphs.

Section 5 SOAP Encoding was the last attempt the translate object graphs into XML. Ultimately it failed because of interoperability problems.
  • XML is inherently hierarchical - therefore it cannot represent graphs with cycles all that well.
  • Even if the server implementation environment can represent objects there is no guarantee the you web service's client can. Furthermore data types that exist on your server platform may not even exist on the client platform. Even if you have types that translate to the same XML Schema type, those types may ultimately have differing semantics on each platform.

  • I need to beat the vendors over the head to stop doing that, to instead make tools, build tools that want to look at and see and recognize that (A) this is a message and (B) it�s in XML because wanting to take your code and seamlessly turning it into objects and back again, we�ve already talked about that. That�s just not going to work. It�s not going to happen.

    Web Services on Drugs: Ted Neward on Web Services Tech Talk: Ted Neward on Web Services and Security

    So basically you have to design the XML representation of the information that you want to exchange first and then use JAXB to get easy Java access to that information.
    Antero Laukkanen

    Joined: Aug 24, 2007
    Posts: 6
    Thanks for your time and well thought reply. I can see you are clearly right there. We are moving towards use of web services that actually makes sense, sending the object graphs in their entirety would have made the transition just a little easier for the time being but it seems we will have to go all the way already (which might not be such a bad idea anyhow). So, thanks again for pointing out the problem, might have taken some time to figure out it was a dead end we were looking at..
    I agree. Here's the link:
    subject: jaxb & object graphs
    jQuery in Action, 3rd edition