Spring Jaxb2Marshaller外部绑定文件

时间:2015-09-01 14:46:23

标签: java spring spring-mvc jaxb unmarshalling

在Spring中,我正在声明我的org.springframework.oxm.jaxb.Jaxb2Marshaller,但我还要声明一个外部绑定文件:

<bean id="myMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.path.to.pojos" />
    <property name="jaxbContextProperties">
        <util:map>
            <entry key="eclipselink.oxm.metadata-source">
                <list>
                    <value>com/path/to/schema/binding.xjb</value>
                </list>
            </entry>
        </util:map>
    </property>
    <property name="schema" value="classpath:com/path/to/schema/myService.xsd"/>
</bean>

我的绑定文件如下所示:

<jaxb:bindings version="1.0" 
    jaxb:version="2.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:annox="http://annox.dev.java.net" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc annox">

    <jaxb:bindings schemaLocation="myService.xsd" node="/xs:schema">

        <jaxb:globalBindings>
            <xjc:javaType name="java.util.Date" xmlType="xs:date"
            adapter="com.some.path.to.custom.adapter.DateAdapter" />
        </jaxb:globalBindings>

        <!-- More Declarations -->

        </jaxb:bindings>
    </jaxb:bindings>

此设置适用于XJC,可以从架构生成对象以及外部绑定文件。但我无法为我的Spring配置获得适当的设置。

我收到以下错误:

org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 77; unexpected     element (uri:"http://java.sun.com/xml/ns/jaxb", local:"bindings"). Expected elements are <{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-schema>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-schema-type>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-schema-types>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-java-type-adapters>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-registries>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}xml-enums>,<{http://www.eclipse.org/eclipselink/xsds/persistence/oxm}java-types>

我坚持这一点,我真的需要绑定文件与我的架构分开。我在网上找不到这个设置的例子,我想知道如何用JaxB2Marshaller正确配置外部绑定文件的例子。

如果我的问题不完整或需要更多信息,请告诉我。

谢谢,

JP

1 个答案:

答案 0 :(得分:1)

As far as I know, bindings file is only used during the compilation time, to derive Java classes from the XML Schema. So it does not make sense to configure it in runtime, on a marshaller. Neither Spring nor JAXB will consider it. All you could have configured with the bindings file is already in your com.path.to.pojos.* classes.