我遇到過類似的問題,但他們似乎沒有提供一個直接的例子。我正在嘗試使用RestTemplate以編碼的URL進行GET。我有一個亞馬遜類助手來生成一個簽名的url,但返回編碼的url,所以當我在RestTemplate中使用它時,它會再次被編碼。我應該輸入到RestTemplate中的url應該是這樣的;java RestTemplate GET請求與編碼的URL
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images,ItemAttributes,Offers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06:47:25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ=
但我的亞馬遜幫手給了我這個;
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Keywords=php&Operation=ItemSearch&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-09-05T06%3A47%3A25.703Z&Signature=dthAE5BwmK2aZmSoIPRBwsPgCNwIv6JnXoqjC0QyRCQ%3D
這是再次編碼打破我的時間戳。我發現我可以很容易地使用字符串替換,但我正在尋找一種更好的方式來做到這一點。
我的代碼看起來像這樣;
SignedRequestsHelper helper;
try {
helper = SignedRequestsHelper.getInstance(this.ENDPOINT, this.ACCESS_KEY_ID, this.SECRET_KEY);
} catch (Exception e) {
return "ERROR";
//e.printStackTrace();
}
String requestUrl = null;
Map<String, String> params = new HashMap<String, String>();
...
requestUrl = helper.sign(params);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ResponseErrorHandler());
...
try {
String response = restTemplate.getForObject(requestUrl, String.class);
} catch (HttpStatusCodeException exception) {
return exception.getStatusCode().toString();
}
好吧,我看到'getForObject'的3個變體。我見過UriComponentsBuilder。假設我有一個編碼網址開始。我如何解碼它? – LogicDev
@ user5500750你不會解碼它,你會將它轉換成一個URI並使用一個接受URI的重載方法。上述措詞可以更清楚嗎? –
除了我已經有一個由助手創建的編碼url。你可以在我的代碼中看到。我已經將我的參數提供給返回url的幫助器。我不想創建一個URI模板字符串,然後將其提供給'getForObject'方法。我是Java新手,所以希望我有道理。我意識到你在說什麼,但把它放到代碼中是問題所在。 – LogicDev