JAX-RS NoMessageBodyWriterFoundFailure [英] JAX-RS NoMessageBodyWriterFoundFailure

查看:259
本文介绍了JAX-RS NoMessageBodyWriterFoundFailure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jax-rs应用程序的方法

the method of my jax-rs application:

@GET
@Produces (MediaType.APPLICATION_JSON)
public List <Document> getDocumentList(@HeaderParam("Range") String headerRange) {
int [] range = getRangeFromHeader(headerRange);
return facade.listByRange(range);
}

正常工作。
但如果修改:

working properly. But If modifications to the:

@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

Server Jboss 7.1.1

请告诉我有什么问题。

PS.sorry我的英语不好。

PS.sorry for my bad English.

推荐答案

下面的代码段可以解决这个问题。

The snippet below should do the trick.

@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 子类需要为编写者提供正确的类型信息(否则由编译器删除)。

The anonymous GenericEntity subclass is required to supply the correct type information (otherwise erased by the compiler) for the writer.

- 编辑

您的代码使用 org.jboss.resteasy.resteasy-jackson-provider 工作的原因,但不是 org.jboss.resteasy.resteasy-jettison-provider 依赖于两家提供商之间的根本区别:

The reason why your code worked using org.jboss.resteasy.resteasy-jackson-provider but not with org.jboss.resteasy.resteasy-jettison-provider resides on the fundamental difference between the two providers:


  • 前者(jackson)依赖于JavaBean模型,发现要序列化的对象的属性,并且不需要类型信息。

  • 后者(jettyson)依赖于JAXB注释,因此需要基础类型信息,由编译器擦除。

这篇关于JAX-RS NoMessageBodyWriterFoundFailure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆