使用的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