我的课程结构如下:
@XmlRootElement(name = "storage")
@XmlType(propOrder = {
"entries"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
@XmlElement(name = "configuration")
private Set<ClassB> entries;
...
}
@XmlRootElement(name = "configuration")
@XmlType(propOrder = {
"name",
"entries"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class B {
private String name;
@XmlElement(name = "configurationEntry")
private Set<ClassC> entries;
...
}
@XmlRootElement(name = "configurationEntry")
@XmlType(propOrder = {
"property1",
"property2",
"property3"
})
@XmlAccessorType(XmlAccessType.FIELD)
public class C {
private String property1;
private String property2;
private String property3;
...
}
当我使用Jersey为我做一个XML时,当我尝试访问根容器(A类)时,我得到以下输出。我不知道为什么标签没有标签。
<Bes>
<configuration>
<name>Name1</name>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
</configuration>
<configuration>
<name>Name2</name>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>..</property2>
<property3>...</property2>
</configurationEntry>
</configuration>
</Bes>
当我尝试访问其中一个ClassB容器时,我得到以下内容。 (与以前类似的问题),而不是我得到,并且没有包含标签。
<Aes>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<Aes>
唯一能按预期工作的是最低级别的C级
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
为了清楚起见,我想要的输出如下:
<storage>
<configuration>
<name>Name1</name>
<entries>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
</entries>
</configuration>
<configuration>
<name>Name2</name>
<entries>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>..</property2>
<property3>...</property2>
</configurationEntry>
</entries>
</configuration>
</storage>
<configuration>
<name>Name1</name>
<entries>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
</entries>
</configuration>
<configurationEntry>
<property1>...</property1>
<property2>...</property2>
<property3>...</property2>
</configurationEntry>
答案 0 :(得分:1)
因为我在A类和B类中看到了标签<Bes>
和<Aes>
。我觉得你已经定义了根元素..我的意思是A类和B类是另一个类的子集。检查这个是因为xml只能有一个根元素。如果是这样,则A类和B类中的根元素无效。我没有看到类中定义的xml有任何问题。有关更多信息,请访问http://java.dzone.com/articles/jaxb-and-root-elements
答案 1 :(得分:0)
没关系,我是个白痴。 -.-'
在我的客户端代码中,出于某种原因,我分别检索Set<B>
和Set<C>
而不是A
和B
。