RestTemplate如何添加請求頭headers和請求體body
RestTemplate添加請求頭headers和請求體body
//headers & cookie HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("basecret", config.getBasecret()); headers.add("baid", config.getBaid()); List<String> cookies = new ArrayList<>(); cookies.add("COOKIE_USER" + Strings.nullToEmpty(config.getCookie())); headers.put(HttpHeaders.COOKIE, cookies);
1、調(diào)用postForObject方法
2、使用postForEntity方法
3、調(diào)用exchange方法
postForObject和postForEntity方法的區(qū)別主要在于可以在postForEntity方法中設(shè)置header的屬性,當需要指定header的屬性值的時候,使用postForEntity方法。
exchange方法和postForEntity類似,但是更靈活,exchange還可以調(diào)用get請求。
使用這三種方法傳遞參數(shù),Map不能定義為以下兩種類型
Map<String, Object> paramMap = new HashMap<String, Object>(); Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
把Map類型換成LinkedMultiValueMap后,參數(shù)成功傳遞到后臺
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("dt", "20190225"); // 1、使用postForObject請求接口 String result = template.postForObject(url, paramMap, String.class); // 2、使用postForEntity請求接口 HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers); ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class); // 3、使用exchange請求接口 ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
4、get方法
如果是get請求,又想要把參數(shù)封裝到map里面進行傳遞的話,Map需要使用HashMap,且url需要使用占位符,如下:
RestTemplate restTemplate2 = new RestTemplate(); String url = "http://127.0.0.1:8081/interact/getData?dt={dt}&ht={ht}"; // 封裝參數(shù),這里是HashMap Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("dt", "20190225"); paramMap.put("ht", "10"); //1、使用getForObject請求接口 String result1 = template.getForObject(url, String.class, paramMap); System.out.println("result1====================" + result1); //2、使用exchange請求接口 HttpHeaders headers = new HttpHeaders(); headers.set("id", "lidy"); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers); ResponseEntity<String> response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
5、如果post請求體是個Json的表單
? ? ? ? //JSONObject userInfo = new JSONObject(); ? ? ? ? Map<String, Object> userInfo = Maps.newHashMap(); ? ? ? ? userInfo.put("phone", accountForm.getPhone()); ? ? ? ? userInfo.put("job", accountForm.getJob()); ? ? ? ? userInfo.put("email", accountForm.getEmail()); ? ? ? ? Map<String, Object> postBody = Maps.newHashMap(); ? ? ? ? postBody.put("userInfo", userInfo); ? ? ? ? HttpEntity<Map> requestEntity = new HttpEntity<>(postBody, headers); ? ? ? ? ?try { ? ? ? ? ? ? ?ResponseEntity<String> result = restTemplate.postForEntity(config.getCreateWithAuthUrl(), requestEntity, String.class); ? ? ? ? ? ? ?JsonNode jsonNode = JsonUtils.toJsonNode(result.getBody()); ? ? ? ? ? ? ?if (jsonNode.get("errno").asInt() == 200 || jsonNode.get("errno").asInt() == 505) { ? ? ? ? ? ? ? ? ?return true; ? ? ? ? ? ? ?} ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? logger.error(e.getMessage(), e); ? ? ? ? }
請求異常RestTemplate 獲取response的headers和body
ResponseEntity<String> responseEntity = null; try { restTemplate.postForEntity("http://localhost:1111/test", null, String.class); } catch (HttpStatusCodeException e) { e.getStatusCode(); e.getResponseBodyAsString(); e.getResponseHeaders(); }
捕捉HttpStatusCodeException替代RestClientException
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項目中controller層與前端的參數(shù)傳遞方式
這篇文章主要介紹了springboot項目中controller層與前端的參數(shù)傳遞方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Java畢業(yè)設(shè)計實戰(zhàn)項目之倉庫管理系統(tǒng)的實現(xiàn)流程
這是一個使用了java+SSM+Maven+Bootstrap+mysql開發(fā)的倉庫管理系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習,具有一個倉庫管理系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01使用springboot aop來實現(xiàn)讀寫分離和事物配置
這篇文章主要介紹了使用springboot aop來實現(xiàn)讀寫分離和事物配置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04