I am having a class BookMain which is returning an arraylist of objects. I am using REST service to get the output, but I am getting 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 which I am getting:
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 anyone provide me a solution for it ?
Introduce a new class as 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 as 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;
}
You need to wrap your entity(Arraylist) under the Response object. Also your rest method return type 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 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;
}
}
You can simply use JSONArray to display the result.
Create a JSONArray object and pass the list to the JSONArray() constructor.
Then return the JSONArray object in string format.
Change the return type of your method to String and the flollowing code would work.
Blockquote
JSONArray a=new JSONArray(list);
return a.toString();