当路由器使用MODE_STARTS_WITH时,Restlet获取剩余路径

时间:2018-04-07 20:18:54

标签: java restlet

我正在创建一个基于Restlet的服务,该服务将传入的请求与特定的初始路径进行匹配。在处理匹配的ServerResource中,我希望访问剩余的路径。

Router parentRouter;
parentRouter.attach("/svc/", new MyService());
parentRouter.attach("/svc/1.0/", new MyService());
public class MyService extends Router
{
  public MetricsService()
  {
    attach("/m/", MyResource.class).setMatchingMode(Template.MODE_STARTS_WITH);
  }
}
public class MyResource extends ServerResource 
{
  @Override
  protected void doInit() throws ResourceException
  {
     String remainingPath = ???   <-- how do I do this?
  }
  @Get
  protected void doit() throws ResourceException
  {
    ...
  }
}

现在,例如,以下请求会产生以下剩余路径:

http://localhost/svc/m/                  -->  ""
http://localhost/svc/m/a                 -->  "a"
http://localhost/svc/v1.0/m/some/m/path  --> "some/m/path"

当然,总有一些解决方法可以解决这个问题,但使用现有的Restlet框架检索它可能会更加优雅。特别是因为路由器已经知道剩余的路径。

任何帮助或建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

经过一番摆弄后,我找到了答案:

@Override
protected void doInit() throws ResourceException
{
   String remainingPath = getReference().getRemainingPart();
}
相关问题