简体   繁体   中英

How to return an arraylist as response to the restful cxf webservice call

I'm trying to retrieve the list of documents from MongoDB using restful cxf webservice call. But I'm facing

No message body writer has been found for response in Class ArrayList.

I had followed this tutorial . Here they are returning employee object as a response in the CxfRestServiceImpl class. So they had used @XMLElement(name = 'employee') .

But now I'm trying to return list of documents from MongoDB as a response in the CxfRestServiceImpl class. What changes I need to do in order to overcome this error?

If I understood you correct, You've got this exception in your code. Than, it is better for you to wrap your List answer in some other class.

@XmlRootElement(name="DocumentList")
public class DocumentList {
    @XmlElement
    public List<Document> documentList;
}

You can "wrap" into Array like this

return Response.status(Response.Status.OK).entity(yourList.toArray(new YourObject[yourList.size()])).build();

where yourList is a List<yourObject> or ArrayList<yourObject>

You can return a list of objects in your service. JAXB will do the conversion from ArrayList

@GET
@Path("/employees")
public List<Employee> getEmployees()

Ensure that the object has the JAXB XmlRootElement annotation.

@XmlRootElement(name="Employee")
public class Employee{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM