2015-12-14 50 views
0

我使用REST模板在請求URI故意發送%禁用編碼,像/items/a%b如何使用RestTemplate

String responseEntity = restTemplate.exchange("/items/a%b", 
      requestObj.getHttpMethod(), requestEntity, String.class); 

restTemplate被轉換此URI的endoding,它正在成爲/items/a%25b這是有道理的因爲默認模板默認編碼uri。

我嘗試使用UriComponent禁用的URI

UriComponents uriComponents = UriComponentsBuilder.fromPath("/items/a%b").build(); 
URI uri= uriComponents.toUri(); 

String responseEntity = restTemplate.exchange(uri, 
      requestObj.getHttpMethod(), requestEntity, String.class); 

的編碼但這不工作作爲URI又是類型URI它做編碼。我確信我沒有正確使用UriComponents。

我真的很感激,如果有人能指出什麼是禁用編碼的正確方法。

謝謝。

+1

您要求發送的請求是非法的。你爲什麼認爲你需要一個懸而未決的百分號? – chrylis

+0

我正在編寫一個測試,使服務器正常處理它。它的全面集成測試將觸及遠程服務器。所以沒有什麼是嘲弄的。 – tintin

+1

服務器*不應該「優雅地」處理它,它應該返回400錯誤。 – chrylis

回答

1

從UriComponentsBuilder DOC,存在方法build(boolean encoded)

build(boolean encoded)構建從包含在該生成器中的 各種部件UriComponents實例。

UriComponents uriComponents = UriComponentsBuilder.fromPath("/items/a%b").build(true); 
相關問題