2013-02-14 28 views
0

比方說,我設置以下的球衣資源:如何獲取Jersey中傳入URL的路徑值?

@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路徑,爲前:

  • GET http://somehost/blah/do/1/then/2應打印/do/1/then/2
  • GET http://somehost/blah/wait/3/then/4應打印/wait/3/then/4
  • GET http://somehost/blah/done/5/done/6應打印/done/5/done/6

當我使用澤西島時,如何獲取此信息?

回答

0

使用@Context HttpServletRequest的請求,以獲取HttpServletRequest的這裏提到 http://jersey.576304.n2.nabble.com/Access-to-Servlet-Request-td5538642.html

,然後使用HttpServletRequest的方法來獲取URL。例如:法getPathTranslated()上的日食

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

+1

確定這是要健全愚蠢的,但我按下熱鍵自動整理並給予我目前的Maven + JAR +依賴性設置...它不能找到HttpServletRequest的導入!我環視了一番,傳遞了'@Context UriInfo allUri'作爲參數,然後評估它爲'System.out.println(allUri.getPath());'做到了! – pulkitsinghal 2013-02-14 19:35:14