Reputation: 49087
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
Upvotes: 8
Views: 38089
Reputation: 21992
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();
}
Upvotes: 3
Reputation: 7472
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.
Upvotes: 6
Reputation: 16663
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
Upvotes: 8
Reputation: 344
Your webservice may look like this:
@GET
@Path("all")
@Produces({ "application/xml", "application/*+xml", "text/xml" })
public Response getAllQuestions(){
List<Question> responseEntity = ...;
return Response.ok().entity(responseEntity).build();
}
then you should create a Provider, MessageBodyWriter:
@Produces({ "application/xml", "application/*+xml", "text/xml" })
@Provider
public class XMLWriter implements MessageBodyWriter<Source>{
}
Upvotes: 0