Monday, February 14, 2011

JAXB 2.1/2.2 and different Java 6 versions

Posting this just in case anyone else hits this issue. We run Java 1.6.0_23 in our local development environments, but deploy to Java 1.6.0_03 (I suppose ideally our development should match the deploy target). I've seen a couple of people encountering problems with this error:

javax.xml.bind.UnmarshalException: Unable to create an instance xmltype ...


I had an abstract JAXB class and some concrete ones that implemented it and were annotated with XmlType. This worked without problems in our local environment, using (I assume) JAXB 2.1. However, when deployed I was getting the above error. Our deploys include JAXB 2.2 in an endorsed folder because we don't want to use JAXB 2.0 (which is what Java versions previous to 1.6.0_04 include).


What a confusing mess! To summarize:
locally: JAXB 2.1
deployed: JAXB 2.2

In the end to fix the issue, I ended up modifying the code that created the JAXBContext to include all of my concrete classes as well (it is using the JAXBContext.newInstance(Class... classes) method). This appears to have fixed it; I'm not sure why 2.1 is able to unmarshal without the classes all listed there while 2.2 is not.

I had read previously that JAXB would use the XmlSeeAlso annotations to look up the additional classes, but it doesn't seem to be behaving as expected in this situation. My abstract class has an XmlSeeAlso annotation that points to all of the concrete implementations.

Hope this helps someone!

No comments: