[英]JAXB and Inheritance with external custom bindings
我目前正在嘗試在XML模式中對Spring LDAP過濾器進行建模。 這涉及多態類型,可以任意嵌套:
<andFilter>
<notFilter>
<equalsFilter name="mail" value="asfd@example.com" />
</notFilter>
<likeFilter name="mail" value="asdf*" />
</andFilter>
這就是我在我的xsd中定義上述過濾器的方式(我實際上定義了一些中間抽象類型,但為了簡單起見將它們排除在外):
<xsd:complexType name="baseFilterType" abstract="true" />
<xsd:element name="filter" type="tns:baseFilterType" />
<xsd:complexType name="andFilterType">
<xsd:sequence>
<xsd:element ref="tns:filter" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="andFilter" type="tns:andFilterType" substitutionGroup="tns:filter" />
<xsd:complexType name="notFilterType">
<xsd:sequence>
<xsd:element ref="tns:filter" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="notFilter" type="tns:notFilterType" substitutionGroup="tns:filter" />
<xsd:complexType name="likeFilterType">
<xsd:sequence>
<xsd:element ref="tns:attributeName" />
<xsd:element ref="tns:value" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="likeFilter" type="tns:likeFilterType" substitutionGroup="tns:filter" />
<xsd:complexType name="equalsFilterType">
<xsd:sequence>
<xsd:element ref="tns:attributeName" />
<xsd:element ref="tns:value" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="equalsFilter" type="tns:equalsFilterType" substitutionGroup="tns:filter" />
我已經使用一些自定義行為擴展了每個過濾器類(以實現訪問者模式),但我無法弄清楚如何將JAXB綁定到那些而不是JAXBElement<? extends BaseFilterType>
JAXBElement<? extends BaseFilterType>
在整個生成的類中使用的JAXBElement<? extends BaseFilterType>
。
JAXB模型類是在構建時生成的,因此我無法直接修改它們 - 我必須使用外部自定義綁定進行任何自定義 。 我已經嘗試過implClass
和baseClass
自定義,但還是無法弄清楚如何做到這一點。
有人可以幫忙嗎?
編輯:我正在使用Spring的Jaxb2Marshaller
其中contextPaths
設置用於編組Jaxb2Marshaller
組。 我希望這可以利用ObjectFactory
進行對象實例化,但似乎並非如此。
編輯2:過濾器元素由頂級元素以及彼此引用,如:
<xsd:element name="GetAttributesRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="filter:filter" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
因此, GetAttributesRequest
對象將使用@XmlRootElement
標記,而過濾器類本身則不會。 但是,即使是頂級的GetAttributesRequest
對象也沒有被ObjectFactory
實例化......
編輯:
問題作者使用的解決方案:
我最終使用JAXB綁定自定義來使ObjectFactory生成我的類的實例,然后在Spring的Jaxb2Marshaller上設置com.sun.xml.internal.bind.ObjectFactory(注意內部)unmarshaller屬性以強制它使用工廠。
原始答案:
是的,可以向生成的類添加行為。 您想查看相關文檔
以下是文檔中的一個簡單示例:
JAXB生成的代碼
package org.acme.foo;
@XmlRootElement
class Person {
private String name;
public String getName() { return name; }
public void setName(String) { this.name=name; }
}
@XmlRegistry
class ObjectFactory {
Person createPerson() { ... }
}
擴展課程
包org.acme.foo.impl;
class PersonEx extends Person {
@Override
public void setName(String name) {
if(name.length()<3) throw new IllegalArgumentException();
super.setName(name);
}
}
@XmlRegistry
class ObjectFactoryEx extends ObjectFactory {
@Override
Person createPerson() {
return new PersonEx();
}
}
解組的例子
Unmarshaller u = context.createUnmarshaller();
u.setProperty("com.sun.xml.bind.ObjectFactory",new ObjectFactoryEx());
PersonEx p = (PersonEx)u.unmarshal(new StringReader("<person />"));
更多信息在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.