JAX-RS NoMessageBodyWriterFoundFailure

我的未来我决定 提交于 2019-12-20 04:46:55

问题


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();
}

I receive an error

...NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response
object of type: java.util.ArrayList of media type: application/json...

Server Jboss 7.1.1

Please tell me what's wrong.

PS.sorry for my bad English.


回答1:


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();
}

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

-- EDIT

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:

  • the former (jackson) relies on a JavaBean model, discovering the properties of the objects to serialize, and needs no type information
  • the latter (jettyson) relies on the JAXB annotations, so it needs the underlying type information, erased by the compiler.



回答2:


You're missing a library as described here:

Here is the solution

This means that you are missing a JSON library in your classpath. Jackson is one I’m using so adding this to your pom.xml will help:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.10.1</version>
</dependency>


来源:https://stackoverflow.com/questions/18613031/jax-rs-nomessagebodywriterfoundfailure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!