亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body

 更新時(shí)間:2023年07月07日 09:16:57   作者:pNull  
這篇文章主要介紹了RestTemplate如何添加請(qǐng)求頭headers和請(qǐng)求體body問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

RestTemplate添加請(qǐng)求頭headers和請(qǐng)求體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的屬性,當(dāng)需要指定header的屬性值的時(shí)候,使用postForEntity方法。

exchange方法和postForEntity類似,但是更靈活,exchange還可以調(diào)用get請(qǐng)求。

使用這三種方法傳遞參數(shù),Map不能定義為以下兩種類型

Map<String, Object> paramMap = new HashMap<String, Object>();
Map<String, Object> paramMap = new LinkedHashMap<String, Object>();

把Map類型換成LinkedMultiValueMap后,參數(shù)成功傳遞到后臺(tái)

MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("dt", "20190225");
// 1、使用postForObject請(qǐng)求接口
String result = template.postForObject(url, paramMap, String.class);
// 2、使用postForEntity請(qǐng)求接口
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請(qǐng)求接口
ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
 

4、get方法

如果是get請(qǐng)求,又想要把參數(shù)封裝到map里面進(jìn)行傳遞的話,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請(qǐng)求接口
String result1 = template.getForObject(url, String.class, paramMap);
System.out.println("result1====================" + result1);
//2、使用exchange請(qǐng)求接口
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請(qǐng)求體是個(gè)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);
? ? ? ? }

請(qǐng)求異常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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論