users@jersey.java.net

Re: [Jersey] Jaxb ArrayList Question

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 18 Jan 2010 11:14:35 +0000

Hi Jonathan,

The item in the list is required to be a JAXB bean annotated with
@XmlRootElement. Unfortunately JAXB does not provide good support for
marshaling primitive data or collections of (to be fair this is never
really a use-case for JAXB/XML but makes a lot of sense for JSON that
is more data, rather than document, oriented).

If you want to support just JSON and lists of stuff like String,
integer etc i recommend you use Jackson which has excellent JSON and
JAX-RS support.

Paul.


On Jan 14, 2010, at 5:38 PM, Jonathan Cook - FM&T wrote:

> Hi,
>
> There are a few threads on this already which I have read but am
> still unsure. So basically I want to use Jaxb to return a list of
> items in json. This works nicely if I create a wrapper class which
> basically just contains a list and is annoted with the
> @XmlRootElement. But I don't really like having to keep creating
> these classes which are essentially just wrappers. Ideally I could
> just populate a list and then return that.
>
> For example:
>
> @GET
> @Produces("application/json")
> @Path("all")
> public Response getAllConfigurations(){
> ArrayList<String> list = new ArrayList<String>();
> list.add("hello");
> list.add("world");
> GenericEntity<List<String>> entity = new
> GenericEntity<List<String>>(list) {};
> Response response = Response.ok(entity).build();
> return response;
> }
>
> I tried playing around with a GeneriEntity class but it still
> complains about there not being a response writer for this type of
> object?
>
> So this is the type of wrapper class I have been creating:
>
> @XmlRootElement
> public class ModuleList {
>
> private List<Module> moduleList;
>
> public ModuleList(){}
>
> public ModuleList(List<Module> moduleList) {
> this.moduleList = moduleList;
> }
>
> public List<Module> getModuleList() {
> return moduleList;
> }
>
> public void setModuleList(List<Module> moduleList) {
> this.moduleList = moduleList;
> }
>
>
> }
>
> Ideally I would like to just populate a List with my Module object
> and return that to save me having to create all these additonal
> classes and populate them.
>
> Thanks
> Jon
>