Im using Jersey to build a REST Service and want to return a Collection<String>
as XML.
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Response getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
Collection<String> result = service.getDirectGroupsForUser(userId, null, true);
// return result; //first try
// return result.toArray(new String[0]); //second try
return Response.ok().type(MediaType.TEXT_XML).entity(result).build(); //third try
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
but my attempts fail with the following exception:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type text/xml was not found
and all results to that exception I found via google dealt with returning text/json instead of text/xml like in my situation.
Can anyone help me? I thought, if I use a Response, that would be my root element in XML and my collection a list of string elements in it..
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…