RestTemplat中getForobject方法的用法

在接短信平臺的接口時,使用了RestTemplate的getForObject方法,自以爲會自動拼接參數,所以只傳了url和保存參數的map,但是多次提交失敗,以爲是中間轉碼出錯

public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        return restTemplate.getForObject(url, responseType, uriVariables);
}

經過查看源碼,發現最後並沒有把參數拼接到url上,然後找了資料才發現了正確用法,尷尬~原來不是自動把map的key和value拼接到url上面,而是需要自己先指定好。即

http://localhost:8080/shortmessageservice.asmx/Send?sysName={sysName}&phoneNumbers={phoneNumbers}&content={content}&priority={priority}

要用佔位符指定到參數對應的key,和自定義的map一樣,使用getForObject方法即可成功把參數拼接上去。

//map的定義
    Map<String,String> request = new HashMap<>();
    request.put("sysName","username");
    request.put("phoneNumbers","13800138000");
    request.put("content","內容");
    request.put("priority", "getforobject");
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章