Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS - Could not find MessageBodyWriter for response object of type

Tags:

jax-rs

I am trying to work out a sample of JAX-RS in JBoss 7.2 Rest Easy. I am getting a Error message as below.

SEVERE [org.jboss.resteasy.core.SynchronousDispatcher] (http-/0.0.0.0:80-2) Failed executing GET /test/resources/1: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: vsample.Image of media type: image/jpeg at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:216) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.writeJaxrsResponse(SynchronousDispatcher.java:602) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:528) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.5.Final.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.5.Final.jar:] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final.jar:1.0.2.Final] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.0.Final.jar:7.2.0.Final] at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_17]

And my code is,

 import javax.ws.rs.ApplicationPath;
     import javax.ws.rs.Produces;
     import javax.ws.rs.core.Application;


     @ApplicationPath("/rest")
   public class Etag extends Application {
    }

And,

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Response.ResponseBuilder;


@Path( "/test" )
public class EtagImpl {

   @GET
   @Produces("image/jpeg")

   @Path("resources/{id}")
      public Response getImage(@PathParam("id") String id, @Context Request request) {

       ImageDAOImpl imageDAO = new ImageDAOImpl();

        Image image = imageDAO.getImage(id);
       CacheControl cc = new CacheControl();
       cc.setMaxAge(172800); // 2 days
       Response.ResponseBuilder rb = null;
       String lastModified = image.getDateModified().toString();
     //  EntityTag etag = new EntityTag(imageDAO.getImage(id).hashCode()+"");
       EntityTag etag = new EntityTag(lastModified.hashCode()+"");
       System.out.println("Writing Response:" + etag);
       rb = request.evaluatePreconditions(etag);
       if (rb != null) 
       {
           return rb.cacheControl(cc).tag(etag).build();
       }

       //If rb is null then either it is first time request; or resource is modified
       //Get the updated representation and return with Etag attached to it
               rb = Response.ok(imageDAO.getImage(id)).cacheControl(cc).tag(etag);
               return rb.build();
    } 

As I am new to RestEasy, it will be helpful to get ideas to proceed in right direction

Thanks and Regards, Ram.

like image 637
user2913892 Avatar asked Dec 19 '22 22:12

user2913892


1 Answers

This simply means that RestEasy does not know how to "convert" your Image object to the mediatype ("image/jpeg") you specified in the @Produces annotation.

By default there's only a limited list of supported types that resteasy can automatically marshal and unmarshal :

  • java.lang.String
  • byte[]
  • java.io.InputStream
  • java.io.Reader
  • java.io.File
  • javax.activation.DataSource
  • javax.xml.transform.Source
  • javax.xml.bind.JAXBElement
  • JAXB classes,
  • javax.ws.rs.core.MultivaluedMap

The easiest way to do it would be to have the Response filled with the byte array representation of your image.

Response.ok(imageDAO.getImage(id).getBytes()).cacheControl(cc).tag(etag);

An other option would be to write you own MessageBodyWriter that will take care of this conversion. Have a look at the the resteasy documentation for more details http://docs.jboss.org/resteasy/docs/3.0.5.Final/userguide/html/Content_Marshalling_Providers.html

like image 94
Pascal Rodriguez Avatar answered Feb 01 '23 23:02

Pascal Rodriguez