14

I am trying to use Jaxb2Marshaller to marshal a set of java classes using spring. I know this can be done using the following code

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.example.test1</value>
            <value>com.example.test2</value>
        </list>
    </property>
</bean>  

What I would like to do is instead of specifying a list of classes, I would like to specify just the package name containing all the classes ( in the above case com.example).

Does anyone know of a way to do this, or any other way which does not require me to list all the classes. any help would be appreciated !

Thanks.

Sandeep More
  • 655
  • 1
  • 6
  • 22

3 Answers3

11

From Spring 3.1 (i think) you can use also the packagesToScan property, which accepts wildcards. It just doesn't work with elements without the @XmlRootElement annotation, just like the contextPath property. These need generated object-factory.

Could look like:

<property name="packagesToScan">
    <list>
        <value>com.test.*</value>
        <value>com.*.test</value>
    </list>
</property>
tomasb
  • 1,663
  • 2
  • 22
  • 29
  • if you are missing the @XmlRootAnnotation and cannot modify the XSD source consider using global jaxb customization see http://stackoverflow.com/a/8725352/881375 – tomasb Jun 02 '13 at 01:32
5

You could set the contextPath with the following syntax:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.example"/>
</bean>  
Lai
  • 1,320
  • 2
  • 13
  • 24
3

If you are using the new version of JAXB then you can use something like this in your application context if you add the oxm namespace to your xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:oxm="http://www.springframework.org/schema/oxm"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/oxm
           http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
  <oxm:jaxb2-marshaller id="jaxbMarshaller" contextPath="com.example"/>
  <!-- other beans -->
</beans>

I have a production level program running with these, so let me know if you have any more questions.

Good Luck.

beerbajay
  • 19,652
  • 6
  • 58
  • 75
thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
  • Thanks ! doesn't this require me to include jaxb.index file in com.yourcompany.yourapp.foo ? – Sandeep More Jan 30 '12 at 18:45
  • What index file do you speak of? I do not have any sort of file in my project. What sort of information is in it? – thatidiotguy Jan 30 '12 at 18:47
  • I ran a small test and I got the following exception: nested exception is javax.xml.bind.JAXBException: "com.yourcompany.yourapp.foo" doesnt contain ObjectFactory.class or jaxb.index. Which lead me to http://stackoverflow.com/questions/899668/how-do-i-use-a-jaxb-index-file – Sandeep More Jan 30 '12 at 18:57
  • Hmmmm, what version of JAXB are you using? For me, the above is all I have in terms of the source code generation in my xml files. I do however have my xsd file in either my WEB-INF or resources folder (Sorry just left the office and no longer have access to my source code). – thatidiotguy Jan 30 '12 at 19:04
  • Oh wait, you know that you have to have JAXB generate the classes first right using the maven xjc plug in. This will generate the java source with all the annotations that JAXB needs as well as the ObjectFactory.class file. Or you can do the annotating yourself, but this really kind of defeats the purpose of using JAXB in the first place. – thatidiotguy Jan 30 '12 at 19:06
  • I don't think xjc is maven specific, it comes with the Java-WS framework (I could be wrong). I don't like generating java source using xjc, instead I would rather prefer to use the "classesToBeBound" property mentioned above. In which case if I add a class to a package I have to modify the XML and update the list. What I would like is a way to specify a package structure and all the classes under that package would automagically bind to the Jaxb2Marshaller. Also, now I know where the ObjectFactory.class came from. thanks :) – Sandeep More Jan 30 '12 at 19:37