@Path与regexp无法正常工作

时间:2014-03-04 18:12:12

标签: java rest resteasy

我正在尝试在我的REST API(我使用的是Resteasy 3.0.2)中构建一个端点,该端点可以在/ catalog / foo / dump或只是/ catalog / dump上进行回答。根据Resteasy文档,这应该有效:

@Path("/catalog)
public class MyResource {

   @GET
   @Path("{var:.*}/dump")
   public String dump() {...}
}

您可以在手册中找到此示例:http://docs.jboss.org/resteasy/docs/3.0.2.Final/userguide/html/Using_Path.html

但是,我没有看到预期的行为。只有/ catalog /(something)/ dump按预期响应。 / catalog / dump引发以下内容:

javax.ws.rs.NotAllowedException: No resource method found for GET, return 405 with Allow header
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:370)
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:113)
at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) 

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我会制作两种方法:

@GET
@Path("/dump")
public String dump1() {
  return dump("");
}

@GET
@Path("/{var:.*}/dump")
public String dump2(@PathParam("var") String var) {
  dump(var);
}