Jakarta ee 如何获取路径';Jersey中传入URL的s值?
假设我设置了以下Jersey资源:Jakarta ee 如何获取路径';Jersey中传入URL的s值?,jakarta-ee,jersey,Jakarta Ee,Jersey,假设我设置了以下Jersey资源: @Path("/blah") public class BlahResource { @GET @Path("/") public String printRelativeUrlPath() { System.out.println("I want to print the relative path that lands us in this method"); System.out.print
@Path("/blah")
public class BlahResource {
@GET
@Path("/")
public String printRelativeUrlPath()
{
System.out.println("I want to print the relative path that lands us in this method");
System.out.println("For Ex: /do/1/then/2");
System.out.println("For Ex: /wait/3/then/4");
System.out.println("For Ex: /done/5/done/6");
}
}
我希望能够打印出到达此方法的相对url路径,例如:
- 获取
应打印http://somehost/blah/do/1/then/2
/do/1/then/2
- 获取
应打印http://somehost/blah/wait/3/then/4
/wait/3/then/4
- 获取
应打印http://somehost/blah/done/5/done/6
/done/5/done/6
当我使用Jersey时,如何获取此信息?使用@Context-HttpServletRequest获取此处提到的HttpServletRequest 然后使用HttpServletRequest中的方法获取URL。例如:方法getPathTranslated()
好的,这听起来很愚蠢,但我按下了eclipse上自动组织的热键,并给出了我当前的maven+jar+依赖项设置。。。找不到HttpServletRequest的导入!我又四处看了看,将
@Context-UriInfo-allUri
作为参数传入,然后将其作为System.out.println(allUri.getPath())进行计算代码>成功了!