Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring RestTemplate Gzip响应缺少内容长度标头_Spring_Spring Boot_Http_Http Headers_Content Length - Fatal编程技术网

Spring RestTemplate Gzip响应缺少内容长度标头

Spring RestTemplate Gzip响应缺少内容长度标头,spring,spring-boot,http,http-headers,content-length,Spring,Spring Boot,Http,Http Headers,Content Length,我使用Spring Boot集成到第三方,该第三方可以提供gzip响应,我想记录压缩响应体与解压缩响应体的大小,以便我们可以审计账单等 我正在使用restemplate.exchange发出GET请求,响应将正确返回,但是我无法在响应中检索内容长度头 我知道第三方正在发回它,因为当我使用Curl发出相同的请求时,我可以看到它: HTTP/1.1 200 OK Date: Mon, 04 Feb 2019 15:40:10 GMT Server: Apache Vary: Accept-Encod

我使用Spring Boot集成到第三方,该第三方可以提供gzip响应,我想记录压缩响应体与解压缩响应体的大小,以便我们可以审计账单等

我正在使用restemplate.exchange发出GET请求,响应将正确返回,但是我无法在响应中检索内容长度头

我知道第三方正在发回它,因为当我使用Curl发出相同的请求时,我可以看到它:

HTTP/1.1 200 OK
Date: Mon, 04 Feb 2019 15:40:10 GMT
Server: Apache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 558
Connection: close
Content-Type: application/json
这是我发出请求并记录长度的Java代码:

final ResponseEntity<String> responseEntity = restTemplate.exchange(url, GET, entity, String.class);
final String body = responseEntity.getBody();
LOG.info("Payload sizes: compressed={}, uncompressed={}", responseEntity.getHeaders().getContentLength(), body.length());
final ResponseEntity ResponseEntity=restemplate.exchange(url、GET、entity、String.class);
最后一个字符串体=responseEntity.getBody();
LOG.info(“有效负载大小:compressed={},uncompressed={}”,responseEntity.getHeaders().getContentLength(),body.length());
对于请求头,我根据第三方的文档设置“Accept Encoding:gzip,deflate”。但是,当我得到响应时,对getContentLength()的调用只返回-1。我在这方面做了很多搜索,并尝试使用ClientHttpRequestInterceptor,但没有乐趣


有人有过同样的问题吗

万一有人遇到与我相同的问题,如果您使用的是TestRestTemplate,它在gzip时不会显示内容编码,请尝试改用RestTemplate

响应中的标题是什么?当您使用
restemplate
发出请求时,服务器可能正在使用分块传输编码。当使用分块编码时,没有
内容长度
标题。是的,我确实检查过,因为我也读过,但这没有意义。这些是我从RestTemplate响应中读取它们时返回的标题
Date=[Mon,04 Feb 2019 14:23:53 GMT],Server=[Apache],Vary=[Accept Encoding],Connection=[close],Content Type=[application/json]
正如您可以看到的那样,与来自Curl的响应相比,我缺少了一些,这相当奇怪。如果有什么问题的话,它看起来像是服务器端的问题。您可以尝试启用HTTP客户端的低级别跟踪,以验证在响应到达客户端后不会丢失标头。好的建议,对我来说也是这样,谢谢,我将尝试:)