Override JAXB annotation Mapping with MOXY oxm binding file

我与影子孤独终老i 提交于 2019-12-22 22:20:14

问题


I'm trying to implement two differents mappings for certains of my objects.

The first mapping is done using JAXB annotations on my classes. It's working fine.

The second mapping is really simple : it should take every Java attribute. To do this I wrote a very simple xml bindings file :

<?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>

The problems come when I try to initialize the 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);
    }

The following Exception occurres

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

For information : The classes OverridedMappingClass extend an abstract class from another package. This abstract class is JAXB annotated, how should I manage it ? Do I have to write an XML for every package where a mapped class exists or is there a way to regroup it in the same file ?

I'm using the 3 following bundles from eclipseLink-plugins

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

Any help is welcomed !


回答1:


You could do the following:

External mapping file (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>

Code to create JAXBContext on external mapping file:

    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);
    }


来源:https://stackoverflow.com/questions/20525192/override-jaxb-annotation-mapping-with-moxy-oxm-binding-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!