users@jersey.java.net

Jaxb ArrayList Question

From: Jonathan Cook - FM&T <Jonathan.Cook2_at_bbc.co.uk>
Date: Thu, 14 Jan 2010 17:38:51 -0000

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