更新时间:2021-08-07 05:39:09
RestTemplate getForObject()
方法不支持设置标题.解决方案是使用 exchange()
方法.
The RestTemplate getForObject()
method does not support setting headers. The solution is to use the exchange()
method.
因此,不要使用 restTemplate.getForObject(url, String.class, param)
(没有标题),而是使用
So instead of restTemplate.getForObject(url, String.class, param)
(which has no headers), use
HttpHeaders headers = new HttpHeaders();
headers.set("Header", "value");
headers.set("Other-Header", "othervalue");
...
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
url, HttpMethod.GET, requestEntity, String.class, param);
最后,使用 response.getBody()
获取结果.
Finally, use response.getBody()
to get your result.
这个问题类似于这个问题.