永久免费看啪啪网址入口_国产精品自产拍在线观看免费_无码99久任我爽精品视频_国语经典毛片黄片_国产精品福利片

當(dāng)前位置: 首頁(yè) > 行業(yè)

Java中restTemplate攜帶Header請(qǐng)求_今日視點(diǎn)

來(lái)源:騰訊云 發(fā)布時(shí)間:2023-03-25 12:12:02 分享至:


(相關(guān)資料圖)

創(chuàng)建請(qǐng)求

創(chuàng)建請(qǐng)求頭:

Map requestBody = new HashMap<>();requestBody.put("userId", userId);requestBody.put("userName", userName);

創(chuàng)建請(qǐng)求體:

HttpHeaders requestHeader = new HttpHeaders();requestHeader.add("cookie", "cookie");requestHeader.add("userInfo", "{userId:101,userName:userName}");

創(chuàng)建請(qǐng)求方式:

HttpEntity> httpEntity = new HttpEntity<>(requestHeader);RestTemplate restTemplate = new RestTemplate();

POST請(qǐng)求

restTemplate發(fā)送POST請(qǐng)求時(shí)可以通過(guò)如下方法獲取ResponseEntity

ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, JSONObject.class);

或用以下方法獲取jsonObject

JSONObject jsonObject = restTemplate.postForObject(url, httpEntity, JSONObject.class);

GET請(qǐng)求

GET請(qǐng)求沒(méi)有相應(yīng)的方法,只能用exchange方法獲取ResponseEntity

ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, JSONObject.class, requestBody);

若出現(xiàn)如下報(bào)錯(cuò):

Not enough variables available to expand

則是因?yàn)镽estTemplate認(rèn)為大括號(hào){}為占位符,需要將請(qǐng)求頭中的{userId:101,userName:userName}改為{\"userId\":\"101\",\"userName\":\"userName\"}

關(guān)鍵詞:

Copyright   2015-2022 太平洋禮儀網(wǎng) 版權(quán)所有  備案號(hào):豫ICP備2022016495號(hào)-17   聯(lián)系郵箱:93 96 74 66 9@qq.com