Friday, 6 August 2010

Being dumb with JAXB and Spring

Yesterday I wasted a coupled of hours and it is my own fault. Just in case you hit the same problem (or in case I hit it again and forget) I'll note it here.

I'm using a mix of JAXB and Spring and I was getting an error message:
javax.xml.bind.JAXBException: "com.mypackage.MyClass" 
    doesn't contain ObjectFactory.class or jaxb.index

This is when Spring tries to load this bean:
<bean 
    class="org.springframework.oxm.jaxb.Jaxb2Marshaller" 
    id="PIMJaxb">
    <property name="contextPaths">
        <list>
            <value>com.mypackage.MyClass</value>
            <value>com.mypackage.MyClass2</value>
        </property>
</bean>

You may have spotted the obvious mistake already but I didn't
The contextPaths property has to be a list of packages not classes.
It is trying to use the class name I have it as a package name and it is trying to find an ObjectFactory class or a jaxb.index file in that package. Since there is no such package then it reports that it cannot find them. Fair enough.

I, however, assumed there was some complication to do with JAXB versions (I have had several of those) and wasted more time than I should have.

For the record, the bean definition needs to be like this:
<bean 
    class="org.springframework.oxm.jaxb.Jaxb2Marshaller" 
    id="PIMJaxb">
    <property name="contextPaths">
        <list>
            <value>com.mypackage</value>
        </property>
</bean>
Post a Comment