users@jersey.java.net

[Jersey] Jersey, jaxb and Lists

From: Sigmar Muuga <meediake_at_gmail.com>
Date: Wed, 27 Apr 2011 18:17:18 +0300

Hello!

I have a class like this:
import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Simple list info holder. Holds the filtered list and its total count
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ListInfo<T> implements Serializable {

private static final long serialVersionUID = 1L;

@XmlElementWrapper(name = "items")
@XmlElement(name = "item")
@XmlElementRef
private List<T> items = null;

private int totalCount;

public ListInfo() {

}

public ListInfo(List<T> list, int totalCount) {
this.items = list;
this.totalCount = totalCount;
}

public List<T> getItems() {
return items;
}

public void setItems(List<T> items) {
this.items = items;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}

And when I try to serialize it with jersey to JSON or XML, I get output like
this:
<listInfo>
<items>
<item/>
<item/>
</items>
<totalCount>2</totalCount>
</listInfo>

{
    items: {
        item: [
            null
            null
        ]
    }
    totalCount: "2"
}

The method in jersey resource is this:
@GET
@Path("countries")
public ListInfo<AreaInfo> findAllCountries() {
return service.findCountries();
}

Now the problem is, why is the list of items containing NULL-s? The item
classes themselves are also annotated as @XmlRootElement-s

Any ideas would be appreciated!

Best regards,
Sigmar