【发布时间】:2017-05-05 03:46:42
【问题描述】:
我已经从 xsd 文件生成了类。
该类包含具有抽象类的字段。这个抽象类有两种不同的实现。我们称之为 Impl1CLass 和 Impl2Class。
我无法修改 xsd 架构,也无法修改生成的类。
我需要做的就是当 jaxb 封送这个类并且抽象字段具有空值时,我需要得到这样的东西:
<dep xsi:type="Impl1Class" xsi:nil="true"/>
生成的类中的这个字段如下所示:
protected Dep dep;
Dep 是抽象类。
所以我需要设置这是 nil 并且类型是特定的(Impl1Class)
我尝试使用 BoundType 作为抽象类创建 XmlAdapter,而 ValueType 是 JAXBElement 但没有运气,因为它需要默认的非参数构造函数,但 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;
【问题讨论】: