使用MOXY oxm绑定文件覆盖JAXB注释映射

时间:2013-12-11 16:55:34

标签: xml jaxb marshalling moxy

我正在尝试为我的某些对象实现两个不同的映射。

第一个映射是在我的类上使用JAXB注释完成的。它工作正常。

第二个映射非常简单:它应该采用每个Java属性。 为此,我写了一个非常简单的xml绑定文件:

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.foo" xml-mapping-metadata-complete="true">
<java-types>
    <java-type name="com.foo.OverridedMappingClass" xml-accessor-type="FIELD">
    </java-type>
    <java-type name="com.foo.OverridedMappingClass2" xml-accessor-type="FIELD">
    </java-type>
</java-types>
</xml-bindings>

当我尝试初始化JAXBContext时出现问题:

    Map<String, Object> props = new HashMap<String, Object>();
    List<String> bindings = new ArrayList<String>();
    bindings.add("com/bindings/bindings-foo.xml");
    props.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
    try
    {
        jaxbContext = JAXBContext.newInstance(new Class[] { OverridedMappingClass, OverridedMappingClass2}, props);
    }
    catch (JAXBException e)
    {
        m_logger.error("Could not build the JAXBContext", e);
    }

发生以下异常

javax.xml.bind.JAXBException: 24
 - with linked exception:
[java.lang.ArrayIndexOutOfBoundsException: 24]
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1059)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:182)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:340)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 24
at org.eclipse.persistence.internal.libraries.asm.Type.getType(Unknown Source)
at org.eclipse.persistence.internal.libraries.asm.Type.getArgumentTypes(Unknown Source)
at org.eclipse.persistence.internal.libraries.asm.MethodWriter.visitMaxs(Unknown Source)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMapEntryClass(MappingsGenerator.java:2003)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMapEntryClassAndDescriptor(MappingsGenerator.java:1941)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateCompositeCollectionMapping(MappingsGenerator.java:2125)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMapping(MappingsGenerator.java:798)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMappings(MappingsGenerator.java:2464)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateMappings(MappingsGenerator.java:2424)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateProject(MappingsGenerator.java:246)
at org.eclipse.persistence.jaxb.compiler.Generator.generateProject(Generator.java:188)
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1064)
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1056)
... 19 more

有关信息:OverridedMappingClass类从另一个包扩展了一个抽象类。这个抽象类是JAXB注释的,我应该如何管理它?我是否必须为存在映射类的每个包编写XML,或者是否有办法在同一个文件中重新组合它?

我正在使用eclipseLink-plugins

中的以下3个捆绑包
org.eclipse.persistence.asm_3.3.1.v201302191223.jar
org.eclipse.persistence.core_2.6.0.v20130912-6b5e067.jar
org.eclipse.persistence.moxy_2.6.0.v20130912-6b5e067.jar

欢迎任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

外部映射文件(bindings-foo.xml):

<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="com.foo" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="OverridedMappingClass"
            xml-accessor-type="FIELD">
        </java-type>
        <java-type name="OverridedMappingClass2"
            xml-accessor-type="FIELD">
        </java-type>
    </java-types>
</xml-bindings>

在外部映射文件上创建JAXBContext的代码:

    Map<String, Object> props = new HashMap<String, Object>(1);
    props.put(JAXBContextProperties.OXM_METADATA_SOURCE, "com/bindings/bindings-foo.xml");
    try
    {
        jaxbContext = JAXBContext.newInstance(new Class[] { OverridedMappingClass, OverridedMappingClass2}, props);
    }
    catch (JAXBException e)
    {
        m_logger.error("Could not build the JAXBContext", e);
    }