Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 207 posts at DZone. You can read more from them at their website. View Full User Profile

Adapters in JAXB

  • submit to reddit

Java XML Binding (aka JAXB) is part of many applications since it provides a convenient API to marshall/unmarshall Java to/from XML.

Like so many area, the devil is in the detail, like when one has to unmarshall a JAXB-incompatible class. Such classes come across one’s code for a variety of reasons: design, legacy, third-party… Suffice to say that such a sand grain can be a blocker for any project.

Luckily, JAXB designers must have had this use-case in mind when they crafted their API, because an adapter is provided to bypass such problems. In this case, a JAXB-compatible class is made available and JAXB bridges between it and the incompatible one. This is easily done through the use of the XmlJavaTypeAdapter annotation: it accepts a class parameter which bridges between the JAXB-friendly and -unfriendly worlds. In other words, it must implement the XmlAdapter<OriginalType, CompatibleType> contract. The annotation can then be put on the field which is of incompatible type.

This is all well, and many resources on the web tells how to do it. Alas, most examples stay on the safe side and completely forget to address the following:

  • My class to be marshalled (and back) has a field of type A
  • Type A has an associations of type B, which in turn has an association of type C, etc
  • All of these types are third-party
  • Type A through Y are JAXB-compatible, type Z is not

Now things get interesting… Creating the adapter for type A is out of the question, since I would have to create the structure for the whole tree, which would be a huge time sink.

The solution I’ve found so far is to create a in the third-party package and annotate it with my adapter. It’s not clean in that it may come in conflict with the real package-info (i.e. the one coming from the third-party library), but it was not the case for me (none was provided). Btw, I’m amazed how few providers are creating this object in their library.

Anyone having faced this problem and found a more elegant solution?



Published at DZone with permission of Nicolas Frankel, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Eric Pederson replied on Mon, 2012/01/30 - 9:26am

This is very interesting. I'd love to see a code example, including the

Matt Young replied on Mon, 2012/01/30 - 10:59am

How about some actual code. Show what you are trying to do. I have spent the last 2 months beating JAXB into submission with a frighteningly high degree of success. If you show your code, I'd be happy to comment on it.

Nicolas Frankel replied on Tue, 2012/01/31 - 2:37am

This is my


import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.