使用 Eclipselink/MOXy 2.3 我在编组到 XML 时有以下用例:
abstract class MyAbstract {
}
class MyImpl extends MyAbstract {
}
class A {
private MyAbstract myAbstract;
// MyImpl is behind this
public MyAbstract getMyAbstract() {
return myAbstract;
}
}
我在 oxm.xml 中定义了以下映射:
<java-type name="foo.MyAbstract" xml-accessor-type="NONE">
<xml-see-also>
foo.MyImpl
</xml-see-also>
</java-type>
<java-type name="foo.MyImpl">
<xml-root-element name="MyImpl" />
</java-type>
<java-type name="bar.A" xml-accessor-type="NONE">
<xml-root-element name="A" />
<java-attributes>
<xml-element java-attribute="myAbstract" type="foo.MyAbstract" />
</java-attributes>
</java-type>
现在这导致:
<A>
<myAbstract xsi:type="myImpl">
<!-- Mapped members of MyImpl + MyAbstract -->
</myAbstract>
</A>
因为我不想要导出的 xml 中的属性名称,所以我更改了:
<java-type name="bar.A" xml-accessor-type="NONE">
<xml-root-element name="A" />
<java-attributes>
<xml-element java-attribute="myAbstract" type="foo.MyAbstract" xml-path="."/>
</java-attributes>
</java-type>
这导致:
<A>
<!-- Members of MyImpl + MyAbstract marshalled without any wrapping element-->
</A>
我想要的是:
<A>
<MyImpl>
<!-- Members of MyImpl + MyAbstract -->
</MyImpl>
</A>
问题是:我如何实现这一目标?MOXy 只是忽略了我在 MyImpl 上的 XmlRootElement ......
编辑:
尝试 Blaise 的建议会给我以下异常:
Exception [EclipseLink-60] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DescriptorException
The method [] or [getMyAbstract] is not defined in the object [bar.A].
现在这需要我之前遗漏的更多信息,因为我认为它不相关:
A 类是一个接口,它定义:public X getMyAbstract();
MyAbstract 实现 X(这就是我在接口 A 的映射中添加类型属性的原因)。
所以,使用xml-element-ref
MOXy 不再“看到”吸气剂,使用xml-element
它。