Does the remainder support arraialist objects?

I have a BookMain class that returns arraylist objects. I use the REST service to get the result, but I get an error.

This is my BookMain class:

@GET @Path("/get") @Produces(MediaType.APPLICATION_XML) public ArrayList<Object> addObjects() { Book book = new Book(); book.setName("The Book"); book.setAuthor("Author"); BookStore bookstore = new BookStore(); bookstore.setName("The Bookstore"); bookstore.setLocation("UK"); ArrayList<Object> list = new ArrayList<Object>(); list.add(book); list.add(bookstore); return list; } 

This is the error I get:

 11 Jul, 2013 3:36:52 PM com.sun.jersey.spi.container.ContainerResponse write SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.ArrayList<java.lang.Object>, and MIME media type application/xml was not found 11 Jul, 2013 3:36:52 PM com.sun.jersey.spi.container. ContainerResponse write SEVERE: The registered message body writers compatible with the MIME media type are:application/xml -> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App 

Can someone provide me a solution for this?

+4
source share
3 answers

Introduce a new class as shown below

 @XmlRootElement(name = "responseList") public class ResposeList { private List<Object> list; public List<Object> getList() { return list; } public void setList(List<Object> list) { this.list = list; } } 

and set the list below

 @GET @Path("/get") @Produces(MediaType.APPLICATION_XML) public ResposeList addObjects() { Book book = new Book(); book.setName("Here is the Game"); book.setAuthor("HHH"); BookStore bookstore = new BookStore(); bookstore.setName("Prateek Bookstore"); bookstore.setLocation("Vasanth Nagar"); ArrayList<Object> list = new ArrayList<Object>(); list.add(book); list.add(bookstore); ResposeList books=new ResposeList(); books.setList(list); return books; } 
+8
source

You need to wrap your entity (Arraylist) under the Response object. Also, your rest method return method should be Response. Here is what you need to do:

 @GET @Path("/get") @Produces(MediaType.APPLICATION_XML) public Response addObjects() { Book book = new Book(); book.setName("Here is the Game"); book.setAuthor("HHH"); BookStore bookstore = new BookStore(); bookstore.setName("Prateek Bookstore"); bookstore.setLocation("Vasanth Nagar"); ArrayList<Object> list = new ArrayList<Object>(); list.add(book); list.add(bookstore); return Response.status(200).entity(list).build(); } 

Also add @XmlElemen t on top of the getter in your ResponseList bean

 @XmlRootElement public class ResponseList { private ArrayList<Object> list; @XMLElement("booksList") public ArrayList<Object> getList() { return list; } public void setList(ArrayList<Object> list) { this.list = list; } } 
+2
source

You can simply use JSONArray to display the result. Create a JSONArray object and pass it to the JSONArray () constructor. Then return the JSONArray object in string format. Change the return type of your method to String, and flollowing code will work.

Blockquote

JSONArray a = new JSONArray (list); return a.toString ();

0
source

Source: https://habr.com/ru/post/1490876/


All Articles