[英]How to get STRING response from RestTemplate postForLocation?
我正在使用Spring Framework中的RestTemplate在Java中創建REST客戶端。
一切都很好,直到我必須使用postForLocation做一個帖子。
我有權訪問的webservice返回一個json,其中包含有關POST ACTION的信息。
在PHP中它很好,但我真的不明白如何使用RestTemplate在Java中做。
public String doLogin()
{
Map<String, String> args = new HashMap<String, String>();
args.put("email", AUTH_USER);
args.put("token", AUTH_PASS);
String result = restTemplate.postForLocation(API_URL + "account/authenticate/?email={email}&token={token}", String.class, args);
return result;
}
這返回NULL。
使用相同的代碼,但使用getForObject (當然,將URL更改為正確的)我有一個完整的響應,即這有效:
String result = restTemplate.getForObject(url, String.class);
那么......如何從postForLocation獲得RESPONSE?
Obs:對不起,如果這個問題很愚蠢。 我是Java的初學者
postForLocation
方法返回Location標頭的值。 您應該使用帶有String
類的postForObject
,它返回服務器的響應。
像這樣:
String result = restTemplate.postForObject(API_URL + "account/authenticate/?email={email}&token={token}", String.class, args);
這將以字符串形式返回響應。
感謝其中一個答案,我已經弄清楚如何使用postForObject從Spring獲取POST的響應
String result = restTemplate.postForObject(API_URL + "account/authenticate/?email="+ AUTH_USER +"&token="+ AUTH_PASS, null, String.class);
出於某種原因,我無法使用MAP參數,並且必須將它們內聯到URL中。 但這對我來說沒問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.