最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

resttemplate設(shè)置params的方法

 更新時(shí)間:2025年04月03日 09:17:05   作者:在下,楊江河  
RestTemplate設(shè)置請(qǐng)求參數(shù)的方式根據(jù)請(qǐng)求類型(GET/POST)和參數(shù)形式(路徑參數(shù)、查詢參數(shù)、JSON請(qǐng)求體)有所不同,下面通過本文給大家介紹resttemplate設(shè)置params的方法,感興趣的朋友一起看看吧

如何使用RestTemplate設(shè)置請(qǐng)求參數(shù)

RestTemplate設(shè)置請(qǐng)求參數(shù)的方式根據(jù)請(qǐng)求類型(GET/POST)和參數(shù)形式(路徑參數(shù)、查詢參數(shù)、JSON請(qǐng)求體)有所不同,以下是具體實(shí)現(xiàn)方法:

一、GET請(qǐng)求參數(shù)設(shè)置

路徑參數(shù)
使用占位符{param},通過Map或可變參數(shù)傳遞:

// 使用Map傳參
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("id", "123");
String result = restTemplate.getForObject("http://example.com/api/{id}", String.class, uriVariables);
// 或使用可變參數(shù)
String result = restTemplate.getForObject("http://example.com/api/{id}", String.class, "123");

查詢參數(shù)
使用UriComponentsBuilder構(gòu)建帶參數(shù)的URL:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/api/data")
    .queryParam("name", "John")
    .queryParam("age", 25);
String url = builder.toUriString();
String result = restTemplate.getForObject(url, String.class);

二、POST請(qǐng)求參數(shù)設(shè)置

JSON請(qǐng)求體
使用HttpEntity封裝嵌套JSON參數(shù),并設(shè)置請(qǐng)求頭:

// 構(gòu)建嵌套參數(shù)
Map<String, Object> paramMap = new HashMap<>();
Map<String, String> queryMap = new HashMap<>();
queryMap.put("c1", "value1");
paramMap.put("a", "valueA");
paramMap.put("b", queryMap);
// 設(shè)置請(qǐng)求頭
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(paramMap, headers);
// 發(fā)送請(qǐng)求
String response = restTemplate.postForObject("http://example.com/api", entity, String.class);

引用示例中的多層嵌套JSON構(gòu)建方式。

表單參數(shù)
使用MultiValueMap傳遞表單數(shù)據(jù):

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("username", "admin");
formData.add("password", "123456");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(formData, headers);
ResponseEntity<String> response = restTemplate.postForEntity("http://example.com/login", entity, String.class);

三、配置RestTemplate超時(shí)(可選)

通過配置類設(shè)置連接和讀取超時(shí):

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(10000); // 10秒
        factory.setReadTimeout(10000);    // 10秒
        return new RestTemplate(factory);
    }
}

引用配置類示例。

四、處理復(fù)雜響應(yīng)

解析JSON響應(yīng)并提取數(shù)據(jù):

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
JSONObject jsonResponse = new JSONObject(response.getBody());
if ("0000".equals(jsonResponse.getJSONObject("parameter").getString("code"))) {
    String result = jsonResponse.getString("result");
}

引用響應(yīng)處理方法。

到此這篇關(guān)于resttemplate設(shè)置params的方法的文章就介紹到這了,更多相關(guān)resttemplate設(shè)置params內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

晋城| 百色市| 哈密市| 贺州市| 西吉县| 隆昌县| 兰考县| 吉安市| 清原| 南江县| 东莞市| 肥城市| 永清县| 沙坪坝区| 呈贡县| 桓台县| 水城县| 修水县| 长宁县| 繁峙县| 江源县| 民县| 高尔夫| 交口县| 滨海县| 隆昌县| 易门县| 霍城县| 建平县| 新余市| 临朐县| 嘉定区| 西丰县| 天气| 平陆县| 平顺县| 宁晋县| 葵青区| 城步| 丹巴县| 婺源县|