Q:

JAX RS auto Marshal/Unmarshal List objects inside root objects

I've a method like following in a service class
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response listUsers( 
        @QueryParam("page") @DefaultValue("1") Integer page,
        @QueryParam("sortFields") @DefaultValue("id") String sortFields,
        @QueryParam("sortDirections") @DefaultValue("asc") String sortDirections) {

    PaginatedListWrapper listWrapper = new PaginatedListWrapper<>();
    listWrapper.setCurrentPage(page);
    listWrapper.setSortFields(sortFields);
    listWrapper.setSortDirections(sortDirections);
    listWrapper.setTotalResults(this.countAll());

    int start = (listWrapper.getCurrentPage() - 1) * listWrapper.getPageSize();
    listWrapper.setList(userFacade.findRange(new int[]{start, listWrapper.getPageSize()}));

    return Response.status(Response.Status.OK).entity(listWrapper).build();
}

my PaginatedListWrapper class like follows
public class PaginatedListWrapper {
private Integer currentPage;
private Integer pageSize;
private Integer totalResults;

private String sortFields;
private String sortDirections;
private List list;

...
and im getting output like the following
Object{ currentPage: 1,  list: Array[2], 0: "net.brac.ict.mis.tup.uplift.business.entities.User[ id=1 ]" 1: "net.brac.ict.mis.tup.uplift.business.entities.User[ id=2 ]" , pageSize: 10,  sortDirections: "asc", sortFields: "id"…}

why im not getting the list of users as json object? like array of objects [{}, {}, {} ...]
what im missing here?
other than using jersey or jackson, i want to learn the underneath core stuffs of doing this.
in my maven it just javaee-web-api and eclipselink

A:

If your User entity is annotated with JAXB annotations, you can do basically this much simpler:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Collection<User> listUsers( 
        @QueryParam("page") @DefaultValue("1") Integer page,
        @QueryParam("sortFields") @DefaultValue("id") String sortFields,
        @QueryParam("sortDirections") @DefaultValue("asc") String sortDirections) {

    PaginatedListWrapper listWrapper = new PaginatedListWrapper<>();
    listWrapper.setCurrentPage(page);
    listWrapper.setSortFields(sortFields);
    listWrapper.setSortDirections(sortDirections);
    listWrapper.setTotalResults(this.countAll());

    int start = (listWrapper.getCurrentPage() - 1) * listWrapper.getPageSize();
    return userFacade.findRange(new int[]{start, listWrapper.getPageSize()});
}

JAX-RS has a built-in Collection MessageBodyWriter provider and will map your entity using JAXB accordingly for you. This is how I return all my entities in my JAX-RS services.

