JAX-RS - 无法为类型的响应对象找到MessageBodyWriter

时间:2013-10-24 02:49:40

标签: jax-rs

我正在尝试在JBoss 7.2 Rest Easy中找出JAX-RS的样本。我收到如下错误信息。

  

SEVERE [org.jboss.resteasy.core.SynchronousDispatcher]   (http- / 0.0.0.0:80-2)执行GET / test / resources / 1失败:   org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:不能   找到类型的响应对象的MessageBodyWriter:vsample.Image of   媒体类型: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]

我的代码是,

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


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

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

由于我是RestEasy的新手,因此让想法朝着正确的方向前进是有帮助的

谢谢和问候, 拉姆。

1 个答案:

答案 0 :(得分:11)

这只是意味着RestEasy不知道如何将Image对象“转换”为@Produces注释中指定的mediatype(“image / jpeg”)。

默认情况下,只有有限的受支持类型列表,resteasy可以自动编组和解组:

  • java.lang.String中
  • 字节[]
  • 的java.io.InputStream
  • java.io.Reader中
  • 的java.io.File
  • javax.activation.DataSource
  • javax.xml.transform.Source
  • javax.xml.bind.JAXBElement
  • JAXB课程,
  • javax.ws.rs.core.MultivaluedMap

最简单的方法是让Response填充图像的字节数组表示。

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

另一种选择是编写自己的MessageBodyWriter来处理这种转换。 有关详细信息http://docs.jboss.org/resteasy/docs/3.0.5.Final/userguide/html/Content_Marshalling_Providers.html

,请查看resteasy文档