这是我从特定 API 检索 JSON 字符串的代码:
RestTemplate restTemplate = new RestTemplate();
String jsonString = restTemplate.getForObject("http://us.turtlepool.space/api/stats", String.class);
它适用于其他几个网站,但在这个特定的网址上它只生成:
HH
nTmâÖë«·ó`N¦ò±t'«SÆe÷âb}
ÆùT4;%g#þj*[Ã<«5·Ì
yTÖ%e¸ìhe7Sµ,9\ÇX.æâësR|¼oñÏ1"%ºÄÆE[.w¿bâMm¤d×2¦÷\Ê25´ègj.YÜ£×Uñmég1ÖÕ]Æ_3¼M_7f}ö6|i)ÍTæOÚìmH5ç¤fbáã
ê51
可能出了什么问题?
最佳答案
当使用 Firefox 查询您的 URL 时,我在响应 header 中看到“Compression=deflate”。这意味着返回的流是 GZIP 编码的。所以,返回的字符串实际上是压缩后的内容,而不是json。我觉得很奇怪,RestTemplate 默认情况下不处理它。
为了使您的示例正常工作,我遵循了 this answer (在本地测试,响应看起来不错):
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
String jsonString = restTemplate.getForObject("http://us.turtlepool.space/api/stats", String.class);
关于java - Spring REST GET 请求生成哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873691/