如何从jersey服务端点发送实体列表? [英] How to send the list of entities from the jersey service endpoint?
本文介绍了如何从jersey服务端点发送实体列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从jersey服务器发送实体列表.在客户端,我正在尝试获取这些实体列表.但这给了元帅非凡的成就.
I am sending the list of entities from the jersey server. At client side i am trying to get those list of entities. But it is giving unmarshal excepiton.
为什么要在元素名称的末尾添加"s",即"emps"而不是"emp".
Why it is adding 's' at the end of elementname i.e "emps" instead of "emp".
@XmlRootElement
public class Emp{
...
}
Server side code:
@POST
@Path("/getallemps")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public List<Emp> getAllEmployees2(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException{
List<Emp> el = new ArrayList<Emp>();
....
return el;
}
client side code:
public void testGetAllEmployees(){
String res = null;
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(uri);
...
ClientResponse response = resource.type(MediaType.APPLICATION_XML).post(ClientResponse.class);
List<Emp> li = response.getEntity(new GenericType<List<Emp>>(Emp.class));
}
Exception is:
....unmarshalexception unexpected element(url="" local="emps") expected element {}emp
推荐答案
而不是response.getEntity(new GenericType<List<Emp>>(Emp.class));
使用response.getEntity(new GenericType<List<Emp>>(){});
不确定为什么前者不起作用,但是我对后者进行了测试,并且效果很好.
Not quite sure why the former does not work, but I tested with the latter and it works fine.
这篇关于如何从jersey服务端点发送实体列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文