2017-05-05 77 views
3

我想爲特定的方法獲取URI而不「硬編碼」它。Jersey:獲取方法的URL

我試圖UriBuilder.fromMethod但它只能產生在@Path註釋爲該方法指定的URI,它沒有考慮到資源類它在的@Path

例如,這裏的類

@Path("/v0/app") 
public class AppController { 

    @Path("/{app-id}") 
    public String getApp(@PathParam("app-id") int appid) { 
     // ... 
    } 

} 

我想獲取getApp方法的URL,例如這個/v0/app/100

UPDATE:

我想從其他方法的URL比getApp

回答

3

如果使用UriBuilder.fromResource它的工作原理,然後用path(Class resource, String method)

URI uri = UriBuilder 
     .fromResource(AppController.class) 
     .path(AppController.class, "getApp") 
     .resolveTemplate("app-id", 1) 
     .build(); 

不知道添加的方法路徑爲何不適用於fromMethod

這是一個測試案例

public class UriBuilderTest { 

    @Path("/v0/app") 
    public static class AppController { 

     @Path("/{app-id}") 
     public String getApp(@PathParam("app-id") int appid) { 
      return null; 
     } 
    } 

    @Test 
    public void testit() { 
     URI uri = UriBuilder 
       .fromResource(AppController.class) 
       .path(AppController.class, "getApp") 
       .resolveTemplate("app-id", 1) 
       .build(); 

     assertEquals("/v0/app/1", uri.toASCIIString()); 
    } 
}