我尝试从资源输出一些文档列表:
@GET
@Produces({MediaType.APPLICATION_XML})
public Response getDocuments(@QueryParam("provider") String provider) {
List<Document> documents = service.getDocuments(provider);
return Response.ok(
new GenericEntity<List<Document>>(
new ArrayList<Document>(documents)) {})
.build();
}
Document 类没有用 @XmlElement 注释(我真的不喜欢用如此低级的东西注释我的好对象......),但我有一个注册的提供程序:
@Service
@Provider
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.WILDCARD})
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.WILDCARD})
public class JaxRsDocumentSerializer extends
AbstractMessageReaderWriterProvider<Document> {
当我使用具有以下代码的客户端调用此资源时:
return getResource().path("/documents")
.queryParam("provider", provider)
.accept(MediaType.APPLICATION_XML).get(new GenericType<List<Document>>() {});
我遇到了可怕的异常:
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException:
A message body writer for Java class java.util.ArrayList, and Java type
java.util.List<com.polyspot.model.core.Document>,
and MIME media type application/xml was not found
我不明白这里出了什么问题,因为我遵循了在其他地方找到的代码,包括在 SO 中。
非常感谢您的帮助。
最佳答案
也许您的 Document
类需要 @XmlRootElement
注释。
或者,如果您的 Jersey 版本 >= 1.2,您可以尝试使用 JResposne
而不是 Response
。这可以避免使用 GenericEntity
关于java - 带 Jersey 和 JAXB 的编码列表<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9907739/