Specify a package instead of “classesToBeBound” for spring Jaxb2Marshaller

前端 未结 3 507
深忆病人
深忆病人 2021-02-13 13:21

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



        
相关标签:
3条回答
  • 2021-02-13 14:00

    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>  
    
    0 讨论(0)
  • 2021-02-13 14:08

    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>
    
    0 讨论(0)
  • 2021-02-13 14:09

    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.

    0 讨论(0)
提交回复
热议问题