我使用@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)