好的。我是 JAXB 的新手,我在使用 XmlID 和 XmlIDREF 时遇到了一些麻烦,其中 ID 位于抽象类中。
这是我的问题:
抽象类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(EntityImpl.class)
public abstract class AbstractEntity {
@XmlID
private String id;
public AbstractEntity() {
this.id = generateStringId();
}
private static String generateStringId() {
return "urn:uuid:" + UUID.randomUUID().toString();
}
}
实现类
@XmlAccessorType(XmlAccessType.FIELD)
public class EntityImpl extends AbstractEntity {
private String variabel_l = "1";
private String variabel_2 = "2";
private String variabel_3 = "3";
private String variabel_4 = "4";
}
某类
此类包含一个 AbstractEntity,在此示例中为一个字符串。
@XmlAccessorType(XmlAccessType.FIELD)
public class SomeClass {
@XmlIDREF
private AbstractEntity ae;
private String status;
private SomeClass() {
}
public SomeClass(AbstractEntity ae, String status) {
this.ae= ae;
this.status = status;
}
}
根
此类包含两个列表,一个包含“SomeClass”实例的列表和一个包含“EntityImpl”实例的列表
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Root {
@XmlElementWrapper
@XmlElement(name = "result")
private List<SomeClass> someClassList = new ArrayList<>();
@XmlElementWrapper
@XmlElement(name = "entityImpl")
private List<EntityImpl> entityImplList = new ArrayList<>();
public void addResult(SomeClass someClass) {
this.someClassList.add(someClass);
}
public void addEntityImpl(EntityImpl EntityImpl) {
this.entityImplList.add(EntityImpl);
}
演示代码
impl 被添加到 res 中,并且 res 和 impl 都被添加到它们各自的根目录中。
public static void main(String[] args) throws JAXBException {
Root root = new Root();
EntityImpl impl = new EntityImpl();
SomeClass res = new SomeClass(impl, "something");
root.addEntityImpl(impl);
root.addResult(res);
JAXBContext JAXB_CONTEXT;
JAXB_CONTEXT = JAXBContext.newInstance(Root.class);
StringWriter writer = new StringWriter();
JAXB_CONTEXT.createMarshaller().marshal(root, writer);
System.out.println(writer.getBuffer().toString());
}
我想要的结果是对 SomeClass-entity 中 impl 的 Id 的引用,但结果是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<someClassList>
<result>
<ae xsi:type="entityImpl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>urn:uuid:b8a4c0e4-330d-4ed7-9b68-1517d2eb4c2a</id>
<variabel_l>1</variabel_l>
<variabel_2>2</variabel_2>
<variabel_3>3</variabel_3>
<variabel_4>4</variabel_4>
<variabel_5>5</variabel_5>
</ae>
<status>something</status>
</result>
</someClassList>
<entityImplList>
<entityImpl>
<id>urn:uuid:b8a4c0e4-330d-4ed7-9b68-1517d2eb4c2a</id>
<variabel_l>1</variabel_l>
<variabel_2>2</variabel_2>
<variabel_3>3</variabel_3>
<variabel_4>4</variabel_4>
<variabel_5>5</variabel_5>
</entityImpl>
</entityImplList>
</root>
如果我将@XmlIDREF 移动到根实体中的 entityImplList,那么引用工作正常,我得到了这个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<someClassList>
<result>
<ae xsi:type="entityImpl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>urn:uuid:46d0681f-4ec6-49ac-8c22-4954eaa5c680</id>
<variabel_l>1</variabel_l>
<variabel_2>2</variabel_2>
<variabel_3>3</variabel_3>
<variabel_4>4</variabel_4>
<variabel_5>5</variabel_5>
</ae>
<status>something</status>
</result>
</someClassList>
<entityImplList>
<entityImpl>urn:uuid:46d0681f-4ec6-49ac-8c22-4954eaa5c680</entityImpl>
</entityImplList>
</root>
当 REF 位于“SomeClass”中时,是否可以让 XmlID / XmlIDREF 工作,或者这不可能?
有任何想法吗?