getAl...">

Returning a List of Objects Using JAX-RS

How do 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: Fixed exception for response: 500 (Internal server error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: post author for Java class java.util.Vector and Java type java.util.List and MIME media application type / octet stream not found

+7
source share
4 answers

Try:

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

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

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

Edit Added original answer above

+4
source

The same problem in my case was solved by adding the initialization parameter POJOMappingFeature 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 list return on Weblogic 12c.

+6
source

First of all, you must set the correct @Produces annotation. Secondly, you can use GenericEntity to serialize the 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>>() {}; return Response.ok(entity).build(); } 
+3
source

Your web service might 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 must create a Provider, MessageBodyWriter:

 @Produces({ "application/xml", "application/*+xml", "text/xml" }) @Provider public class XMLWriter implements MessageBodyWriter<Source>{ } 
0
source

All Articles