我有一个具有 List 成员的对象链接,而链接只有属性,但列表的解析有问题 - 它被创建为空。
在下面的测试中links.getLinks()
返回空列表。有任何想法吗?
XML 示例:
<links>
<link x="1" y="2" />
<link x="3" y="4" />
</links>
爪哇
@JacksonXmlRootElement(localName="links")
public class Links extends BaseAmebaElement {
@JacksonXmlProperty(localName="link")
//@JacksonXmlElementWrapper(localName="link")
private Collection<Link> links;
public Collection<Link> getLinks() {
return links;
}
public void setLinks(Collection<Link> links) {
this.links = links;
}
}
...
@JacksonXmlRootElement(localName="link")
public class Link {
@JacksonXmlProperty(localName="x", isAttribute=true)
private String href;
@JacksonXmlProperty(localName="y", isAttribute=true)
private String rel;
...
XmlMapper xmlMapper = new XmlMapper ();
try {
Links links = xmlMapper.readValue(input, Links.class);
assertNotNull(links);
assertNotNull(links.getLinks());
assertEquals(2, links.getLinks().size());
} catch (Throwable e) {
fail(e.getMessage());
}