簡體   English   中英

JAXB生成的類字段是抽象的

[英]JAXB generated class field is abstract

我已經從xsd文件生成了類。

此類包含具有抽象類的字段。 這個抽象類有兩個不同的實現。 我們稱之為Impl1CLass和Impl2Class。

我無法修改xsd模式,也無法修改生成的類。

我需要做的就是當jaxb封送此類和abstract字段具有null值時,我需要獲取以下內容:

<dep xsi:type="Impl1Class" xsi:nil="true"/>

生成的類中的Thi字段如下所示:

protected Dep dep;

Dep是抽象類。

所以我需要設置為nil並且類型是特定的(Impl1Class)

我試圖用BoundType作為抽象類創建XmlAdapter,並且ValueType是JAXBElement,但是沒有運氣,因為它需要默認的非arg構造函數,但是JAXBElement沒有這樣的構造函數。

備注。 換句話說,我想在xsi:nil =“ true”時設置xsi:type。 我怎樣才能做到這一點?

這是生成的類

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "DepartmentKey"
)
@XmlSeeAlso({GroupDepartmentKey.class, EnterpriseDepartmentKey.class})
public abstract class DepartmentKey {
    public DepartmentKey() {
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "GroupDepartmentKey",
    propOrder = {"serviceProviderId", "groupId", "name"}
)
public class GroupDepartmentKey extends DepartmentKey {
    @XmlElement(
        required = true
    )
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String serviceProviderId;
    @XmlElement(
        required = true
    )
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String groupId;
    @XmlElement(
        required = true
    )
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String name;
    ......
}

在目標類中,沒有任何注釋的字段

protected DepartmentKey department;

撥打電話時,可以使用setter方法設置null。

Impl1Class impl1Class = new Impl1Class();
imple1Class.setDep(null);

可以使用二傳手方法嗎? 如果不是,則可能需要子類Impl1Class並將Dep設置為null。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM