4

我需要在 Restlet 资源类中获取应用程序根目录(它扩展了 ServerResource)。我的最终目标是尝试将完整的显式路径返回到另一个资源。

我目前正在使用getRequest().getResourceRef().getPath(),这几乎可以满足我的需求。这不会返回完整的 URL(如http://example.com/app),它会返回给我/resourceName。所以我遇到了两个问题,一个是它缺少架构(http 或 https 部分)和服务器名称,另一个是它没有返回应用程序安装到的位置。

因此,给定一个位于“ http://dev.example.com/app_name/person ”的人员资源,我想找到一种方法来找回“ http://dev.example.com/app_name ”。

我正在使用 Restlet 2.0 RC3 并将其部署到 GAE。

4

4 回答 4

2

它看起来getRequest().getRootRef().toString()给了我我想要的东西。我尝试使用getRequest().getRootRef()(如getPathgetRelativePart)的方法调用的组合,但他们给了我一些我不想要的东西或null

于 2010-04-30T03:31:45.107 回答
1

只需从服务上下文中获取基本 url,然后与资源共享它并在需要时添加资源路径。

MyServlet.init():

String contextPath = getServletContext().getContextPath();
getApplication().getContext().getAttributes().put("contextPath", contextPath);

MyResource:

String contextPath = getContext().getAttributes().get("contextPath");
于 2010-11-11T07:05:53.570 回答
1

request.getRootRef()还是request.getHostRef()

于 2010-04-29T20:18:15.613 回答
0

servlet 的上下文可从 restlet 的应用程序访问:

org.restlet.Application app = org.restlet.Application.getCurrent();
javax.servlet.ServletContext ctx = ((javax.servlet.ServletContext) app.getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"));
String path = ctx.getResource("").toString();
于 2014-10-14T21:59:19.840 回答