使用的EclipseLink/MOXY 2.3我已经在编组到XML以下用例:JAXB-的EclipseLink:XmlRootElement将和继承
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>
的问题是:我如何实现这一目标?莫西只是忽略我的MyImpl XmlRootElement将...
编辑:
试图布莱斯什么建议给了我以下异常:
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)的映射中添加了type-attribute。
因此,使用xml-element-ref
MOXy不再“看到”吸气剂,使用xml-element
它。
A类是一种接口? A映射到XML的具体实现是什么? – 2012-01-16 13:18:31
@BlaiseDoughan:我没有针对具体类的映射 - 只是接口bar.A的映射。由于我只是通过界面传递,我认为仅仅为界面创建映射就足够了。 我遵循http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html中的第一个示例。 – quaylar 2012-01-16 13:33:22
在这个例子中,假设接口'A'对应于我的例子中的'Customer'接口,在我的例子中,您仍然需要引导并映射JAXBContext到impl类,即CustomerImpl。 – 2012-01-16 13:46:10