小编Kon*_*kin的帖子

XmlID/XmlIDREF和最新JAXB版本(Java 7)中的继承

我使用@XmlID和@XmlIDREF标记来引用另一个对象.即使使用继承的类,它在Java 6中也能正常工作.我创建的示例代码如下所示.基类使用的标签:

@XmlRootElement
@XmlAccessorType(FIELD)
public class Module {
    Module() {}

    @XmlIDREF
    private Module other;

    @XmlID
    private String id;

    public Module(String id, Module other) {
        this.id = id;
        this.other = other;
    }
}
Run Code Online (Sandbox Code Playgroud)

继承类:

@XmlRootElement
public class TheModule extends Module {
    TheModule() {}

    private String feature;

    public TheModule(String id, Module other, String feature) {
        super(id, other);
        this.feature = feature;
    }
}
Run Code Online (Sandbox Code Playgroud)

这些类的容器:

@XmlRootElement
public class Script {
    Script() {}
    public Script(Collection<Module> modules) {
        this.modules = modules;
    }

    @XmlElementWrapper
    @XmlElementRef
    Collection<Module> modules …
Run Code Online (Sandbox Code Playgroud)

java jaxb xml-serialization java-7

5
推荐指数
1
解决办法
1750
查看次数

标签 统计

java ×1

java-7 ×1

jaxb ×1

xml-serialization ×1