2012-07-17 40 views
0

如何從Response.class中獲取資源URI路徑/位置?當我用這樣的Apache CXF客戶端API調用我的寧靜服務時:REST - 從響應中獲取資源URI(NOT ClientResponse)

Response res = resource.post(object); 

我找回JAX-RS響應類型。 CXF並不像澤西島或者RestEasy那樣自己實現響應。那麼如何從Response.class中獲取URI,我在哪裏創建了我的對象?

在澤西我正在處理ClientResponse.class。在那裏我可以處理這個:

res.getLocation(); 

RestEasy也有一個ClientResponse.class以及我可以處理像澤西島一樣的問題。通過getMetadata()

/** 
* Get the location. 
* 
* @return the location, otherwise <code>null</code> if not present. 
*/ 
public URI getLocation() { 
    String l = getHeaders().getFirst("Location"); 
    return (l != null) ? URI.create(l) : null; 
} 

的JAX-RS響應提供標題信息:

回答

4

澤西ClientResponse會從報頭中的Location

public MultivaluedMap<String, Object> getMetadata() { 
    if (headers != null) 
     return headers; 

    headers = new OutBoundHeaders(); 

    for (int i = 0; i < values.length; i++) 
     if (values[i] != null) 
      headers.putSingle(ResponseBuilderHeaders.getNameFromId(i), values[i]); 

    Iterator i = nameValuePairs.iterator(); 
    while (i.hasNext()) { 
     headers.add((String)i.next(), i.next()); 
    } 

    return headers; 
} 

所以,我會嘗試是:

response.getMetadata().getFirst("Location"); 

(如果這不起作用打印Met adata內容。也許鑰匙有另一個名字。)

+0

它不適用於Jersey 2.8。標題中沒有URI。這可能是因爲重定向? – 2014-05-21 07:15:46