![](/img/trans.png)
[英]How to implement push-style RESTful webservice with Apache CXF?
[英]How to return an arraylist as response to the restful cxf webservice call
我正在嘗試使用restful cxf webservice調用從MongoDB中檢索文檔列表。 但是我面對
在類ArrayList中找不到響應的消息正文編寫器。
我遵循了本教程 。 在這里,他們在CxfRestServiceImpl
類中返回雇員對象作為響應。 因此他們使用了@XMLElement(name = 'employee')
。
但是現在我試圖從MongoDB返回文檔列表作為CxfRestServiceImpl
類的響應。 為了克服此錯誤,我需要做哪些更改?
如果我理解您的正確,那么您的代碼中就有這個異常。 比起將您的“列表”答案包裝在其他班級上,這是更好的選擇。
@XmlRootElement(name="DocumentList")
public class DocumentList {
@XmlElement
public List<Document> documentList;
}
您可以像這樣“包裝”到數組中
return Response.status(Response.Status.OK).entity(yourList.toArray(new YourObject[yourList.size()])).build();
其中yourList是List<yourObject>
或ArrayList<yourObject>
您可以返回服務中的對象列表。 JAXB將執行ArrayList的轉換
@GET
@Path("/employees")
public List<Employee> getEmployees()
確保對象具有JAXB XmlRootElement批注。
@XmlRootElement(name="Employee")
public class Employee{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.