Monday 26 November 2007

JAXB ClassCastException

JAXB marshalling and unmarshalling throws runtime/classcast exception

If you search the web, you will encounter lots of discussions about what causes a class cast exception in JAXB when you try and implement the NamespacePrefixMapper and then call marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", ... ).

However, the simplest cases are not always discussed.

We encountered this problem with marshalling xml in an EJB3 environment and it turned out to be a genuine class cast exception as follows:

Code was being called from a servlet. However, the Web Server was using JAX-WS which was using a different version of JAXB to the back-end. (sharing the libraries during deployment).

The solution (quick hack for us) was to get the Servlet to call a session bean which was using the correct version of JAXB. Obviously at some point we should ensure consistency across the board, but for anyone who is getting a ClassCastException when using NamespacePrefixMapper - check the jars first ;)

No comments: