问题
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