[英]How to marshall a set with jaxb?
I have the following code: 我有以下代码:
@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo
{
@XmlElement
private String id;
...
}
I would like to be able to marshal a Set<Foo> foos
into: 我希望能够将Set<Foo> foos
为:
<foos>
<foo>
<id>bar1</id>
</foo>
<foo>
<id>bar2</id>
</foo>
</foos>
Do I need a wrapper class? 我需要包装课吗? If so, how should it look? 如果是这样,它应该看起来如何? Are my annotations correct? 我的注释正确吗? How should the marhalling code look like (if you could illustrate this all, that'd be much appreciated)? 编组代码应如何显示(如果您能说明所有这些,将不胜感激)?
If you want to encapsulate any Collection use XmlElementWrapper 如果要封装任何Collection,请使用XmlElementWrapper
@XmlElementWrapper(name="foos")
@XmlElement(name="foo")
private Set<Foo> foos;
By the way you cannot directly marshall a Set so you have to include your Set inside your own class. 顺便说一下,您不能直接编组Set,因此必须将Set包含在自己的类中。 So if you just want to marshall a set of Foo you have to write a bean like this : 因此,如果您只想编组一组Foo,则必须编写这样的bean:
@XmlRootElement(name = "foos")
public class Foos {
@XmlElement(name="foo")
private Set<Foo> foo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.