我正在尝试使用 Jersey 传递一个 long 数组:
在客户端我尝试过类似的东西:
@GET
@Consume("text/plain")
@Produces("application/xml)
Response getAllAgentsById(@params("listOfId") List<Long> listOfId);
有没有办法实现类似的东西?
提前致谢!
最佳答案
如果你想坚持“application/xml”格式并避免使用 JSON 格式,你应该将这些数据包装到一个 JAXB 注释对象中,这样 Jersey 就可以使用内置的 MessageBodyWriter/MessageBodyReader .
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public ListOfIds{
private List<Long> ids;
public ListOfIds() {}
public ListOfIds(List<Long> ids) {
this.ids= ids;
}
public List<Long> getIds() {
return ids;
}
}
在客户端(使用 Jersey 客户端)
// get your list of Long
List<Long> list = computeListOfIds();
// wrap it in your object
ListOfIds idList = new ListOfIds(list);
Builder builder = webResource.path("/agentsIds/").type("application/xml").accept("application/xml");
ClientResponse response = builder.post(ClientResponse.class, idList);
关于java - REST - 如何使用 Jersey 传递一个 long in 参数数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4878043/