严重[org.jboss.resteasy.core.SynchronousDispatcher] (http-/0.0.0.0:80-2) 执行 GET /test/resources/1 失败: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:无法 找到 MessageBodyWriter 类型为 vsample.Image 的响应对象 媒体类型:image/jpeg at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:216) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.core.SynchronousDispatcher.writeJaxrsResponse(SynchronousDispatcher.java:602) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:528) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.5.Final.jar:] 在 org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.5.Final.jar:] 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final.jar:1.0.2.Final] 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.0.Final.jar:7.2.0.Final] 在 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 的新手,获得想法以朝着正确的方向前进将会很有帮助
感谢和问候, 拉姆.
默认情况下,resteasy 可以自动编组和解组的支持类型的数量有限:
Response.ok(imageDAO.getImage(id).getBytes()).cacheControl(cc).tag(etag);
另一种选择是编写您自己的 MessageBodyWriter 来处理此转换。 查看resteasy文档以获取更多详细信息
http://docs.jboss.org/resteasy/docs/3.0.5.Final/userguide/html/Content_Marshalling_Providers.html
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>