天天看點

如何正确使用RestTemplate【十】

複習一下下

上篇文章,我們學習了DELETE請求的相關方法的使用方法,以及具體參數的不同,當然還有一些代碼示例等等,你是否還有些印象呢?

RestTemplate今日知識

今天我們來學習OPTIONS請求的使用方法,來共同學習一下吧。

請求方法參數分析

OPTIONS請求

共有參數介紹:

url:通路連結Url,不過多解釋。

uriVariables:url關聯的一些參數

OPTIONS請求沒有傳回值和資源對象,參數個數和傳輸方式和DELETE請求相同,是以也就不需要傳入參數值類型了。

比起get、post少了傳回參數類型這個參數。

比起put請求少了資源對象的相關參數。

optionsForAllow方法

1.

public Set<HttpMethod> optionsForAllow(String url, Object... uriVariables)

此方法需要傳輸url、參數值兩個參數,直接上代碼示例:

restTemplate.optionsForAllow(url, "first param","two param");
for(HttpMethod method : set){
    Sysout.out.println(method);
}      

2.

public Set<HttpMethod> optionsForAllow(String url, Object request, Map<String, ?> uriVariables)

此方法需要傳輸url、Map參數值兩個參數,直接上代碼示例:

Map<String,String> map = new HashMap<>;
map.put("Frist","first param");
map.put("Two","two param");
Set<HttpMethod> set = restTemplate.optionsForAllow(url, map);
for(HttpMethod method : set){
    Sysout.out.println(method);
}      

3.

public Set<HttpMethod> optionsForAllow(URI url)

此方法需要傳輸url即可。

使用場景

OPTIONS請求可以用來動态處理通路連結的請求方式,比如根據optionsForAllow方法擷取到通路連結所支援的請求方式,随後通過判斷來決定使用哪一類來進行調用。

小結

今天我們又學習了OPTIONS請求相關方法的使用方式,你是否有所收獲呢?

繼續閱讀