4

我正在使用 Java Jersey 1.x 来编组一个具有多个成员的对象,其中一个是列表。所有成员变量都被正确编组并以正确的返回类型返回。但是,它不包含objectList在返回数据中。

例子:

@XmlRootElement
public class ClassWithList {
    private String front;
    private int total;
    private ArrayList<AnotherPOJOObject> objectList;
...
getters/setters

吸气剂:

public List<AnotherPOJOObject> getObjectList() {
    return objectList;
}

我对其进行了调试并检查了 objectList 确实填充了数据。 AnotherPOJOObject也被注释为XmlRootElement

4

3 回答 3

1

看看http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlAccessorType.html。它详细说明了 JAXB 将如何尝试序列化 POJO。特别要注意的是,它仅默认为公共成员 - 这意味着“每个公共 getter/setter 对和每个公共字段都将自动绑定到 XML,除非由 XmlTransient 注释”。在这种情况下,我猜测您没有 objectList 的公共 setter 字段,因此 JAXB 不会序列化它。要使列表序列化,您可以:

  • 为 objectList 添加公共 setter 方法
  • 将 objectList 声明为 public(可能不是一个好主意)
  • 向 getter添加@XmlElement注释以明确告诉 JAXB 将列表编组为 XML。
于 2012-09-14T17:31:20.893 回答
1

我遇到了同样的问题,经过反复试验后解决了。

Try giving the annotation @XmlElementWrapper(name = "orders") to getObjectList() and also make the type to private List<AnotherPOJOObject> objectList;

于 2012-09-15T06:02:59.737 回答
0

Thanks to the suggestion to basiljames, I was able to get closer to the answer. The real issue was that the List of AnotherPOJOOject wasn't so plain after all. Each object had an untyped Map of it's own, and that threw the Marshaller into a fit, because, it always wants to know the type of an object.

I guess the takeaway from this answer to make sure that every collection you return has a well defined type!

于 2012-09-16T14:44:59.660 回答