users@jersey.java.net

[Jersey] Re: Jersey, jaxb and Lists

From: Sigmar Muuga <meediake_at_gmail.com>
Date: Fri, 29 Apr 2011 19:39:42 +0300

Hi!

I actually found the problem. When I used List interface, then JAXB was not
able to serialize items there. When I used ArrayList with generics like "?"
or "Object" or "? extends MyEntityBaseClass", also the arraylist with its
items was not serialized.
Using ArrayList with JAXB annotated class generics worked like this:
ArrayList<AreaInfo> list;

I am also thinking about using XStream or similar XML serialization library
in my MessageBodyWriter because there seem to be a lot lesser hassle with
different kind of objects..

Sigmar

On Fri, Apr 29, 2011 at 6:19 PM, Jakub Podlesak
<jakub.podlesak_at_oracle.com>wrote:

> 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
>
>
>