users@jersey.java.net

[Jersey] Re: Jersey, jaxb and Lists

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 29 Apr 2011 17:19:10 +0200

Hi Sigmar,

Even the XML document contains "no data" (see the empty <item/> tags)
and with the JAXB based JSON representation nulls is exactly what
you end up with for empty tags.

If the actual data contains empty String, the POJO
mapping feature should give you better result, something like:
"items" : [ "", "" ]
I.e. if you are complaining about the JSON representation only,
the POJO mapping feature [1] should give you the answer.

But if also the XML representation is broken, you may want (in addition)
to add the AreaInfo class to your JAXB context as suggested by Jason
and see if it solves the issue.

~Jakub

[1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e894

On 04/27/2011 05:17 PM, Sigmar Muuga wrote:
> 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