public JSONObject getVideoList(int page, int perPage) throws IOException
{
String url = "http://10.248.1.12:11021/connect/Token";
String videoListUrl = "http://10.248.1.15:3078/api/Cameras/mediarecords";
HttpHeaders headers = new HttpHeaders();
org.springframework.http.MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded");
headers.setContentType(type);
headers.set("Authorization","Basic QTMwRUQxMTEtQjg1QS00MzBBLTg5OEUtNEJBQjVFNDFDNkMzOkEzMEVEMTExLUI4NUEtNDMwQS04OThFLTRCQUI1RTQxQzZDMw==");
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("grant_type", "password");
form.add("username", "sd01");
form.add("password", "Nucleus!123");
//用HttpEntity封装整个请求报文
HttpEntity<MultiValueMap<String, String>> files = new HttpEntity<>(form, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, files, String.class);
String result =response.getBody();
JSONObject jsStr = JSONObject.parseObject(result);
Object accessToken = jsStr.get("access_token");
headers.set("Authorization","Bearer " + accessToken);
ResponseEntity<String> responseVideo = restTemplate.exchange(
videoListUrl + "?page=" + page + "&per_page=" + perPage,
HttpMethod.GET,
new HttpEntity<String>(headers),
String.class);
String videoResult =responseVideo.getBody();
JSONObject videoInfo = JSONObject.parseObject(videoResult);
return videoInfo;
}
使用RestTemplate实现携带header+参数的http跨服务访问API接口
于 2023-04-21 17:10:12 首次发布