2014-10-16 57 views
2

即時通訊使用澤西島2,我想獲得URI模板。 原因是我創建一個驗證系統,驗證基於URI。我設法工作:澤西島獲得模板路徑

@Override 
    public void filter(ContainerRequestContext containerRequest) throws IOException { 

     String method = containerRequest.getMethod(); 
     String uri = containerRequest.getUriInfo().getPath(); 
    } 

的問題是,的getPath回報是這樣的:

/companies/1 

而且我希望它返回

/companies/{id} 

這就是我如何與聲明:

@Path("{id}") 

謝謝

編輯我想我在這裏找到:

@Context 
private ExtendedUriInfo uriInfo; 

//... 
Resource matchedModelResource = uriInfo.getMatchedModelResource(); 
System.out.println(matchedModelResource.getPathPattern().getTemplate().getTemplate()); 

buut你猜怎麼着? matchedModelResource爲空。

而且,這樣的:

List<UriTemplate> matchedTemplates = uriInfo.getMatchedTemplates(); 

返回UriTemplate的空列表。 :( 爲什麼不將數據beeing設置

回答

2

好所以答案是使用:?

uriInfo.getMatchedTemplates(); 

哪裏uriInfo實際上是ExtendedUriInfo

這是我要做出的代碼。得到正確的語法:

List<UriTemplate> matchedTemplates = uriInfo.getMatchedTemplates(); 

    StringBuilder builder = new StringBuilder(); 

    for (int i = matchedTemplates.size() - 1; i >= 0; i--) { 
     builder.append(matchedTemplates.get(i).getTemplate().substring(1)); 
    } 

    System.out.println("Permission is: " + builder.toString()); 
    // Prints: 
    // Permission is: sig/companies/{id} 

之前數據爲空或空的原因是因爲我的過濾器分類中有@PreMatching註釋秒。請不要問爲什麼。

希望這可以幫助別人。