JAXB-Eclipselink:XmlRootElement 和继承
使用 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 不再“看到” getter,而使用 xml-element
却可以。
Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML:
abstract class MyAbstract {
}
class MyImpl extends MyAbstract {
}
class A {
private MyAbstract myAbstract;
// MyImpl is behind this
public MyAbstract getMyAbstract() {
return myAbstract;
}
}
I have following mapping defined in 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>
Now this results in:
<A>
<myAbstract xsi:type="myImpl">
<!-- Mapped members of MyImpl + MyAbstract -->
</myAbstract>
</A>
Since i didnt want the property-name in the exported xml I changed:
<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>
which resulted in:
<A>
<!-- Members of MyImpl + MyAbstract marshalled without any wrapping element-->
</A>
What i want is:
<A>
<MyImpl>
<!-- Members of MyImpl + MyAbstract -->
</MyImpl>
</A>
The question is: how do i achieve this? MOXy is just ignoring my XmlRootElement on MyImpl...
EDIT:
Trying what Blaise suggested gives me following exception:
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].
Now this needs further information which i had left out before because i thought it was not relevant:
Class A is an interface which defines: public X getMyAbstract();
MyAbstract implements X (this is why i added the type-attribute in the mapping for interface A).
So, using xml-element-ref
MOXy does not "see" the getter anymore, using xml-element
it does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要查找的映射是
@XmlElementRef
。这对应于 XML 模式中替换组的概念。bar/oxm.xml
下面是
bar
包的外部映射文档。请注意 myAbstract 属性如何与xml-element-ref
映射,它是@XmlElementRef
foo/oxm.xml
的 XML 表示形式,下面是
foo
包的外部元数据文件:演示
以下是此示例的演示代码:
输出
了解更多信息
The mapping that you are looking for is
@XmlElementRef
. This corresponds to the concept of substitution groups in XML schema.bar/oxm.xml
Below is the external mapping document for the
bar
package. Note how the myAbstract property is mapped withxml-element-ref
which is the XML representation of@XmlElementRef
foo/oxm.xml
Below is the external metadata file for the
foo
package:Demo
Below is demo code for this example:
Output
For More Information