Search code examples
javajakarta-eeejbjax-rs

Return a list of objects when using JAX-RS


How can I return a list of Question objects in XML or JSON?

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

I get this exception:

SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.Vector, and Java type java.util.List, and MIME media type application/octet-stream was not found


Solution

  • 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