users@jersey.java.net

Re: [Jersey] Unmarshalling List of strings

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 30 Jun 2009 11:38:57 +0200

On Jun 30, 2009, at 10:50 AM, Marko Novakovic wrote:

> Hi,
>
> I have managed to unmarshall list of JAXB-annotated objects from
> JSON using technique described in some of the earlier posts(Bu using
> providers etc...). However, how can i unmarshall list of Strings or
> Integers. For example ["string1", "string2"] to List<String>. I
> would like to stay in JSON-XML independent mode.

This is not currently supported. Note that JAXB does not support such
mechanisms, otherwise we would use that. A JAXB extensions library to
support stuff like this would be very useful.

You will have to write your own message body readers to support
List<T> for the types T you require for XML and JSON. You could use
Jackson for JSON.

Alternatively you might be able to wrap the List<T> around JAXB beans.

I dunno if JAXB will support this:

  public class AbstractListBean<T> {
    List<T> l;

    public AbstractListBean(List<T> l) {
       this.l = l;
    }
  }


  @GET
  AbstractListBean<String> get() {
     List<String> l = ...
     return new AbstractListBean<String>(l) {};
  }

JAXB may actually barf on the use of an anonymous inner class and you
may have to define a class, say StringListBean explicitly.

Paul.