How to send the list of entities from the jersey service endpoint?

孤街浪徒 提交于 2019-12-24 03:20:57

问题


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.

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

回答1:


Instead of response.getEntity(new GenericType<List<Emp>>(Emp.class));

use 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.



来源:https://stackoverflow.com/questions/29647238/how-to-send-the-list-of-entities-from-the-jersey-service-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!