我使用Jersey来构建REST服务并希望返回一个Collection<String>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());
}
}
Run Code Online (Sandbox Code Playgroud)
但我的尝试失败,出现以下异常:
javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java类java.util.ArrayList的消息体编写器,Java类型类java.util.ArrayList,MIME媒体类型text/xml不是发现
我发现通过google处理的返回text/json而不是text/xml就像我的情况一样.
谁能帮我?我想,如果我使用Response,那将是我在XML和我的集合中的根元素,其中包含一个字符串元素列表.