【发布时间】:2013-03-15 03:01:59
【问题描述】:
我正在使用 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());
}
}
但我的尝试失败并出现以下异常:
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。
谁能帮助我?我想,如果我使用响应,那将是我在 XML 中的根元素,而我的集合是其中的字符串元素列表..
【问题讨论】: