当我使用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: {...} }
发布于 2019-06-20 03:27:48
我能够通过将RestTemplate类的日志级别更改为debug来解决我的问题。将logging.level.org.springframework.web.client.RestTemplate=DEBUG
添加到application.properties
,您将获得RestTemplate执行的请求的详细日志。这帮助我注意到它正在编码我第二次传递的url,这是我不想要的。要避免这种情况,请将uriComponents.toUri()
传递给restTemplate.getForEntity(..)
,而不是字符串。
https://stackoverflow.com/questions/56670818
复制相似问题