resttemplate設(shè)置params的方法
如何使用RestTemplate設(shè)置請求參數(shù)
RestTemplate設(shè)置請求參數(shù)的方式根據(jù)請求類型(GET/POST)和參數(shù)形式(路徑參數(shù)、查詢參數(shù)、JSON請求體)有所不同,以下是具體實現(xiàn)方法:
一、GET請求參數(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請求參數(shù)設(shè)置
JSON請求體
使用HttpEntity
封裝嵌套JSON參數(shù),并設(shè)置請求頭:
// 構(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è)置請求頭 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Map<String, Object>> entity = new HttpEntity<>(paramMap, headers); // 發(fā)送請求 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è)置連接和讀取超時:
@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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot3 RestTemplate配置與使用詳解
- SpringBoot中restTemplate請求存在亂碼問題的解決方法
- RestTemplate報錯400 Bad Request的解決方案
- restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
- Resttemplate上傳文件500異常的原因及解決方法
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- RestTemplate發(fā)送form-data請求上傳rul資源文件及對象參數(shù)方式
- springboot中RestTemplate配置HttpClient連接池詳解
相關(guān)文章
IDEA 中 maven 的 Lifecycle 和Plugins&n
IDEA 主界面右側(cè) Maven 標(biāo)簽欄有同樣的命令,比如 install,既在 Plugins 中存在,也在 Lifecycle中存在,到底選哪個?二者又有什么區(qū)別呢?下面小編給大家介紹下IDEA 中 maven 的 Lifecycle 和Plugins 的區(qū)別,感興趣的朋友一起看看吧2023-03-03Java中的static關(guān)鍵字用法總結(jié)
這篇文章主要介紹了Java中的static關(guān)鍵字用法總結(jié),static是Java50個關(guān)鍵字之一,static關(guān)鍵字可以用來修飾代碼塊表示靜態(tài)代碼塊,修飾成員變量表示全局靜態(tài)成員變量,修飾方法表示靜態(tài)方法,需要的朋友可以參考下2023-11-11MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能
這篇文章主要介紹了MyBatis實現(xiàn)動態(tài)查詢、模糊查詢功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06JAVA實現(xiàn)遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用)
本篇文章主要介紹了JAVA 遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用) ,具有一定的參考價值,有興趣的可以了解一下。2017-01-01SpringSecurity中@PermitAll與@PreAuthorize的實現(xiàn)
@PermitAll和@PreAuthorize都是處理安全性的強大工具,本文主要介紹了SpringSecurity中@PermitAll與@PreAuthorize的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-07-07