确定。我是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,在此示例中为String。
@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添加到root上的各自列表中。
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实体中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移动到root-entity中的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工作?或者这是不可能的?
有什么想法吗?
答案 0 :(得分:1)
当我在你的问题中完全按照你的方式运行代码时,我得到以下内容,这不是你得到的输出吗?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<someClassList>
<result>
<ae>urn:uuid:b34f3b9e-1086-4dfc-852f-4bc55b5d5a6e</ae>
<status>something</status>
</result>
</someClassList>
<entityImplList>
<entityImpl>
<id>urn:uuid:b34f3b9e-1086-4dfc-852f-4bc55b5d5a6e</id>
<variabel_l>1</variabel_l>
<variabel_2>2</variabel_2>
<variabel_3>3</variabel_3>
<variabel_4>4</variabel_4>
</entityImpl>
</entityImplList>
</root>
<强>更新强>
基于我自己和ØrjanJohansen的调查,在JDK 1.7.0_21 for Windows中包含的JAXB的实现中似乎存在与此用例相关的错误。用例在Mac的JDK 1.7.0_21和Windows的jdk1.6.0_45中正常工作。当EclipseLink MOXy用作JAXB (JSR-222)提供程序时,此用例也可正常工作。