EclipseLink Moxy:Java类型绑定不适用于子类型(组合)

时间:2014-01-28 12:50:07

标签: java jaxb moxy

我偶然发现MOXy v2.5.1的一个问题,最好用下面的例子解释

输入:

<Root>
    <parentId>1</parentId>
    <parentVersion>1</parentVersion>
    <children> <!-- Always has one and only one child -->
        <child>
            <cType>P</cType>
            <cId>2</cId>
            <cVersion>2</cVersion>
        </child>
    <children>
</Root>

使用上面的XML作为基本输入并使用下面给出的绑定文件,在运行unmarshaller之后,所有java属性(尤其是 cType )都是非null。所以,我们在这里很好。

<?xml version="1.0"?>
<xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="com.xyz.model"
        xml-access-order="alphabetical" 
        xml-mapping-metadata-complete="true">
    <xml-schema/>
    <xml-java-type-adapters>
        <xml-java-type-adapter value="com.xyz.unmarshal.xml.DateAdapter" type="java.util.Date"/>
        <xml-java-type-adapter value="com.xyz.unmarshal.xml.BooleanAdapter" type="java.lang.Boolean"/>        
    </xml-java-type-adapters>
    <java-types>
        <java-type name="com.xyz.model.SomeType">
            <xml-root-element name="Root"/>
            <java-attributes>
                <xml-element java-attribute="id"                            name="parentId"/>
                <xml-element java-attribute="version"                       name="parentVersion"/>
                <xml-element java-attribute="cType"                         name="children/child/cType"/>
                <!--xml-element java-attribute="subType"                        name="children/child" --/>
            </java-attributes>
        </java-type>
        <java-type name="com.xyz.model.SubType">
            <java-attributes>   
                <xml-element java-attribute="subTypeId"         name="cId"/>
                <xml-element java-attribute="subTypeVersion"    name="cVersion"/>
            </java-attributes>
        </java-type>
   </java-types> 
</xml-bindings>

但是,在上面的示例中我们取消注释 subType 属性的那一刻,除 cType 之外的所有内容都已填充。如果我们有任何以与 subType 属性的名称相同的名称开头的话,就会出现这种情况。

知道如何解决这个问题(我肯定无法更改输入XML)?

更新(2月3日):

有一个非常糟糕的解决方案 - 将绑定文件拆分为两个(在我的情况下)并使用不同的绑定文件解组相同的消息两次(昂贵)并将对象合并回来。我对我提出的解决方案并不满意。因此要么需要探索其他一些参考实现,要么等待在MOXy(理想)中解决这个问题。

问候。

1 个答案:

答案 0 :(得分:1)

问题在于元数据文档的以下部分:

<xml-element java-attribute="cType" name="children/child/cType"/>
<xml-element java-attribute="subType" name="children/child"/>

当您将subType属性映射到children/child路径时,它期望映射到child的域对象拥有该元素下面的内容。由于cType的映射低于该元素,因此事情正在被抛弃。