RestTempLate 请求头、参数、请求体设置
参考:https://blog.csdn.net/qq_35642849/article/details/103821900
应用场景:通过微信公众号access_token接口获取到token,然后查询公众号的文章列表。
//region 2.获取文章列表
String articleUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
//设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
//设置参数
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add("access_token", accessToken);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(articleUrl).queryParams(queryParams);
//设置请求体
Map<String, String> params = new HashMap<>();
params.put("type", "news");
params.put("offset", "0");
params.put("count", "1");
String jsonData = JSON.toJSONString(params);
HttpEntity<String> request = new HttpEntity<>(jsonData, httpHeaders);
String queryResult = restTemplate.postForObject(builder.toUriString(), request, String.class);
//endregion