java Spring RestTemplate GET请求未给出正确响应
当我使用RestTemplate执行get请求时,服务器的响应与我通过curl或浏览器获得的响应不同
我尝试过使用URL编码,并将URL记录到控制台以进一步排除故障,但无法解释这种差异
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(URL + "search/all")
.queryParam("keywords", UriUtils.encode(person, StandardCharsets.UTF_8))
.build(true);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(uriComponents.toUriString(), String.class);
RestTemplate的结果URL
:
https://example.com/api/v3/search/all?keywords=%C3%89tienne%20Racicot
返回响应:
{}
表演:
curl https://example.com/api/v3/search/all?keywords=%C3%89tienne%20Racicot
返回响应:
{Person: {...} }
# 1 楼答案
我可以通过更改RestTemplate类的日志记录级别来解决我的问题。将
logging.level.org.springframework.web.client.RestTemplate=DEBUG
添加到application.properties
,您将获得RestTemplate执行的请求的详细日志。这让我注意到,它对我第二次传递的url进行了编码,这是我不想要的。避免将uriComponents.toUri()
传递到restTemplate.getForEntity(..)
而不是字符串