springboot中RestTemplate發(fā)送HTTP請求的實現(xiàn)示例
一、前言
之前對發(fā)送http請求,使用過okhttp,還有httpclient等,這次就直接使用springboot的RestTemplate了。
二、不同的請求方式
我這里只針對POST請求做說明:
下面針對post請求做三種說明:
1、調(diào)用的接口參數(shù)用@RequestParam標識
此種在APIfox中傳參方式是地址欄傳參數(shù),沒有請求體。
/** * 發(fā)送沒有請求體的請求 * @param url 請求url * @param reqParam 地址欄參數(shù) * @return 想要結(jié)果 */ public static JSONObject httpPostResult(String url, Map<String, Object> reqParam) { String params = getParameter(reqParam); if (StringUtils.isBlank(params)) { log.error("獲取sign失敗,sign=null"); return null; } HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(setHeaders()); //解析請求結(jié)果 String result = new RestTemplate().postForObject(url + params, httpEntity, String.class); return JSONObject.parseObject(result); } /** * 對地址欄的參數(shù)進行拼接 * @param map 地址欄參數(shù) * @return 拼接后的結(jié)果 */ public static String getParameter(Map<String, Object> map) { try { if (MapUtil.isEmpty(map)) { return null; } String result = ""; for (String key : map.keySet()) { if (Objects.nonNull(map.get(key) )) { if (!"".equals(result)) { result += "&"; } result = result + key + "=" + map.get(key); } } return result; } catch (Exception e) { log.error("map轉(zhuǎn)參出錯", e); } return null; } /** * 設(shè)置請求頭 * @return 請求頭 */ private static HttpHeaders setHeaders() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString()); httpHeaders.add("Accept-Charset", "UTF-8"); return httpHeaders; }
在Apifox中調(diào)用如下:
請求體body=none。
對應(yīng)的curl命令如下:
curl --location --request POST 'http://xxx:8080/tree/getTree?param1=abc¶m2=123¶m3=def' \
2、調(diào)用的接口參數(shù)用@RequestBody標識
此種在APIfox中傳參方式是地址欄需要傳參數(shù),且有請求體。
/** * 發(fā)送帶有地址欄參數(shù),有請求體的請求 * @param url 請求 url * @param reqParam url上的參數(shù) * @param reqBody 請求體 * @return 響應(yīng)結(jié)果 */ public static JSONObject httpPostResult(String url, Map<String, Object> reqParam, Map<String, Object> reqBody) { String params = getParameter(reqParam); if (StringUtils.isBlank(params)) { log.error("獲取sign失敗,sign=null"); return null; } //封裝請求頭和內(nèi)容 HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(reqBody, setHeaders()); //解析請求結(jié)果 String result = new RestTemplate().postForObject(url + params, requestEntity, String.class); return JSONObject.parseObject(result); }
Apifox上我就只展示body了:
Params和上面的沒有請求體的一樣。
curl命令如下:
curl --location --request POST 'http://xxx:8080/tree/getTree?param1=abc¶m2=123¶m3=def' --header 'Content-Type: application/json' \ --data-raw '{"parentId":80}'
這個命令里面的data-raw的值就是請求體。
3、調(diào)用的接口參數(shù)沒有標識
這種的是所有的參數(shù)都使用form-data的格式進行傳輸:
這種就必須使用LinkedMultiValueMap對參數(shù)進行封裝:
/** * 發(fā)送form-data請求 * @param url 請求 url * @param data 請求體 * @return 響應(yīng)結(jié)果 */ public JSONObject httpPostResult(String url, MultiValueMap<String, String> data) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //解析請求結(jié)果 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(data, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class); return JSONObject.parseObject(response.getBody()); }
對應(yīng)的controller中不需要任何注解:
@PostMapping("/postRequest") public JSONObject intervalData(ChargeLogIntervalDataReq request) { String url = "http://xxx:8081/api/list"; MultiValueMap<String, String> data = new LinkedMultiValueMap<>(); data.put("page", Lists.newArrayList(request.getPage())); data.put("limit", Lists.newArrayList(request.getLimit())); return httpUtils.httpPostResult(url, data); }
這里要注意,MultiValueMap的value得是list才行。
到此這篇關(guān)于springboot中RestTemplate發(fā)送HTTP請求的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot RestTemplate發(fā)送HTTP請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用RestTemplate發(fā)送http請求的實操演示
- SpringBoot使用RestTemplate實現(xiàn)HTTP請求詳解
- springboot中RestTemplate配置HttpClient連接池詳解
- 基于springboot的RestTemplate、okhttp和HttpClient對比分析
- SpringBoot 利用RestTemplate http測試
- 關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴}
- SpringBoot使用RestTemplate如何通過http請求將文件下載到本地
相關(guān)文章
使用RocketMQTemplate發(fā)送帶tags的消息
這篇文章主要介紹了使用RocketMQTemplate發(fā)送帶tags的消息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Javaweb動態(tài)開發(fā)最重要的Servlet詳解
動態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質(zhì)是Java中的一個類(面向?qū)ο螅┻@個類的功能十分強大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實現(xiàn)類實例化的對象才能被瀏覽器訪問,所以掌握Servlet具有重要意義2022-08-08Mybatis-plus解決兼容oracle批量插入的示例詳解
Mybatis-Plus 是一個 MyBatis 的增強工具,提供無侵入、損耗小的 CRUD 操作,本文給大家介紹了Mybatis-plus解決兼容oracle批量插入,文中通過大家介紹的非常詳細,需要的朋友可以參考下2024-11-11在springboot中注入FilterRegistrationBean不生效的原因
這篇文章主要介紹了在springboot中注入FilterRegistrationBean不生效的原因及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08