Return a list of objects when using JAX-RS

Try:

@Path("all")
@GET
public ArrayList<Question> getAllQuestions() {
    return (ArrayList<Question>)questionDAO.getAllQuestions();
}

If your goal is to return a list of item you can use:

@Path("all")
@GET
public Question[] getAllQuestions() {
    return questionDAO.getAllQuestions().toArray(new Question[]{});
}

Edit Added original answer above


The same problem in my case was solved by adding the POJOMappingFeature init param to the REST servlet, so it looks like this:

<servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

Now it even works with returning List on Weblogic 12c.


First of all, you should set proper @Produces annotation. And second, you can use GenericEntity to serialize a list.

@GET
@Path("/questions")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response read() {

    final List<Question> list; // get some

    final GenericEntity<List<Question>> entity
        = new GenericEntity<List<Question>>(list) {};

    return Response.ok(entity).build();
}