如何像 JAX-RS(CXF 和 Jersey)那样使用 JAXB 封送 java.util.List
似乎最新的 JAX-RS 可以处理返回 java.util.List 作为 XMLRootElement 的方法,但普通的 JAXB 不能。 我想模仿 CXF 和 Jersey 正在做的事情。
换句话说,我想编组一个列表,就像 CXF 和 Jersey 一样。 通常,如果您尝试使用 JAXB 封送列表,则会出现根元素异常。 如何解决这个问题而无需制作包装对象?
编辑:感谢您的许多回答,但我非常熟悉@XmlElementWrapper,但这甚至无法模拟 JAX-RS 正在做的事情。
JAX-RS 这样做:
@XmlRootElement(name="dog")
public class Dog {
private String name;
public String getName() { return this.name; }
//Setter also
}
现在如果我序列化狗列表:
serialize(List<Dog> dogs);
XML 应该是(JAX-RS 所做的):
<dogs>
<dog><name>Rascal</name></dog>
</dogs>
所以你可以看到我不想为每个域对象创建一个包装器对象。
It seems the latest JAX-RS can handle methods returning java.util.List as the XMLRootElement but normal JAXB cannot.
I would like to mimic what CXF and Jersey are doing.
In other words I would like to Marshal a List and just like CXF and Jersey do.
Normally if you try to marshal a list with JAXB you get the Root Element exception.
How do I get around this with out having to make a wrapping object?
EDIT: Thanks for the many answers but I'm very familiar with the @XmlElementWrapper but that does not even come close to simulating what JAX-RS is doing.
JAX-RS does this:
@XmlRootElement(name="dog")
public class Dog {
private String name;
public String getName() { return this.name; }
//Setter also
}
Now if I serialize a list of dogs:
serialize(List<Dog> dogs);
XML should be (what JAX-RS does):
<dogs>
<dog><name>Rascal</name></dog>
</dogs>
So you can see I don't want to have to make a wrapper object for every single domain object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你能不能简单地添加:
不需要制作包装对象。这将成为编组 XML 响应中的路由元素。
Could you not simply add:
No need to make a wrapper object. This will then be the route element in your marshalled XML response.
我使用以下代码使用了自定义可迭代列表,希望这会有所帮助。
I used a custom iterable list using the following code, hope this helps.
我有一个秘密,可以让我完全避免使用 JAXB 映射,让一切都神奇地工作。
多年来一直使用这种方法,从来没有花 5 分钟担心编组/解编组。秘密是......不要使用 JAXB。 :)
在我使用 JAX-RS 的大多数项目中,我将 Jersey 配置为使用 xstream 并让 xstream 找出如何为我编组/解组。 (或者 JSON 的 jackson)
也许有一些原因使用 JAXB 而不是 xstream/jackson 之类的东西,但我还没有找到。
I have a secret which allows me to avoid screwing with JAXB mappings entirely and have everything work magically.
Have used this approach for years, never spent even 5 minutes worrying about marshalling/unmarshalling. The secret is.... don't use JAXB. :)
In most of my projects which use JAX-RS, I configure Jersey to use xstream and let xstream figure out how to marshal/unmarshal for me. (or jackson for JSON)
Maybe there are some reasons to use JAXB instead of something like xstream/jackson, but I haven't found any yet.
映射列表可以这样完成..
mapping a List can be done like this..
检查 Jackson,它与 JAXB 绑定非常兼容,并且使用 MappingJsonFactory 实际上可以互换地使用 Java 到 XML 到 Java 到 Json 到 Java。
check Jackson out, it is very compatible with JAXB bindings and using MappingJsonFactory you can actually interchangeably use Java to XML to Java to Json to Java.
我使用
@XmlJavaTypeAdapter(value = Adapter.class)
注释我的集合。Adapter 类扩展自
XmlAdapter
,键是(un)编组的唯一标识符,值是域对象。也许这可以帮助你。但是,您必须为每个域对象创建一个适配器。
I annotate my collections with
@XmlJavaTypeAdapter(value = Adapter.class)
.The Adapter class extends from
XmlAdapter<key, value>
the key is a unique identifier to (un)marshal and the value is your domain object.Maybe this can help you out. However you do have to create an adapter for every domain object.
我相信 Blaise Doughan 在这里展示了解决此问题的一个很好的解决方案:是否可能以编程方式配置 JAXB?
和
http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html
和这里虽然略有不同的用例:
http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html
Blaise Doughan I believe shows a good solution for this problem here: Is it possible to programmatically configure JAXB?
and
http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html
and somewhat here albeit slightly different use case:
http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html