方法 我的 jax-rs 应用程序:
@GET
@Produces (MediaType.APPLICATION_JSON)
public List <Document> getDocumentList(@HeaderParam("Range") String headerRange) {
int [] range = getRangeFromHeader(headerRange);
return facade.listByRange(range);
}
工作正常。 但如果修改为:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getDocumentList(@HeaderParam("Range") String headerRange) {
int[] range = getRangeFromHeader(headerRange);
return Response.ok(
facade.listByRange(range))
.header("Content-Range", getContentRangeStr(range)).build();
}
我收到错误
...NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response
object of type: java.util.ArrayList of media type: application/json...
服务器Jboss 7.1.1
请告诉我出了什么问题。
PS.抱歉我的英语不好。
最佳答案
下面的代码片段应该可以解决问题。
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getDocumentList(@HeaderParam("Range") String headerRange) {
int[] range = getRangeFromHeader(headerRange);
return Response.ok(
new GenericEntity<List<Document>>( (List<Document>)facade.listByRange(range))
)
.header("Content-Range", getContentRangeStr(range)).build();
}
匿名GenericEntity
子类需要为编写器提供正确的类型信息(否则会被编译器删除)。
-- 编辑
您的代码使用 org.jboss.resteasy.resteasy-jackson-provider
工作但不使用 org.jboss.resteasy.resteasy-jettison-provider
的原因在于这两个提供程序之间的根本区别:
- 前者 (jackson) 依赖于 JavaBean 模型,发现要序列化的对象的属性,并且不需要类型信息
- 后者 (jettyson) 依赖于 JAXB 注释,因此它需要底层类型信息,并由编译器删除。
关于java - JAX-RS NoMessageBodyWriterFoundFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613031/