简体   繁体   English

JAXB生成的类字段是抽象的

[英]JAXB generated class field is abstract

I have generated class from xsd file. 我已经从xsd文件生成了类。

This class contains field with abstract class. 此类包含具有抽象类的字段。 This abstract class has two different implementations. 这个抽象类有两个不同的实现。 Let's call is Impl1CLass and Impl2Class. 我们称之为Impl1CLass和Impl2Class。

I cannot modify xsd schemas, I cannot modify generated classes. 我无法修改xsd模式,也无法修改生成的类。

All I need to do is when jaxb marshal this class and abstract field has null value, I need to get something like this: 我需要做的就是当jaxb封送此类和abstract字段具有null值时,我需要获取以下内容:

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

Thi field in generated class looks like this: 生成的类中的Thi字段如下所示:

protected Dep dep;

Dep is abstract class. Dep是抽象类。

So I need to set that this is nil and type is specific one (Impl1Class) 所以我需要设置为nil并且类型是特定的(Impl1Class)

I tried to create XmlAdapter with BoundType as abstract class and ValueType is JAXBElement but there was no luck since it reuires default non-arg constructor but JAXBElement doesn't have such one. 我试图用BoundType作为抽象类创建XmlAdapter,并且ValueType是JAXBElement,但是没有运气,因为它需要默认的非arg构造函数,但是JAXBElement没有这样的构造函数。

REMARK. 备注。 Other words I would like to set xsi:type when xsi:nil="true". 换句话说,我想在xsi:nil =“ true”时设置xsi:type。 How can I do that? 我怎样才能做到这一点?

Here are generated classes 这是生成的类

@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;
    ......
}

In the target class there field without any annotations 在目标类中,没有任何注释的字段

protected DepartmentKey department;

You can set null using setter method while making the call. 拨打电话时,可以使用setter方法设置null。

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

Is setter method available with you? 可以使用二传手方法吗? If not, you might need to subclass Impl1Class and set Dep to null. 如果不是,则可能需要子类Impl1Class并将Dep设置为null。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM