我正在尝试解压从REST服务收到的gzip:ed响应:
Content-Encoding=[gzip], Content-Type=[application/json], Content-Length=[710] ...我正在使用Grails REST客户端Builder插件:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept "application/json"
} 返回的响应是一个SpringResponseEntity。我试图使用GZIPInputStream解压缩数据:
String body = response.getBody()
new GZIPInputStream(new ByteArrayInputStream(body.getBytes())).text这不能用于Caused by ZipException: Not in GZIP format
显然我做错了什么,但我想不出是什么。所有的建议都是见效的。
发布于 2015-07-03 18:58:54
如果确实需要继续使用,只需稍微修改客户端代码:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept byte[].class, "application/json" }注意accept call - byte[].class中的额外参数,这意味着RestTemplate应该避免对响应进行任何解析。
要减压,现在可以这样做:
new GZIPInputStream(new ByteArrayInputStream(response.body))是的,我知道,已经回答了接受,但有些人可能仍然会发现,如果切换Rest组件不是一种选择。
发布于 2015-02-12 14:51:44
我从未设法让它与grails / groovy库一起工作,因此我切换到了spring和httpcomponents:
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
ResponseEntity<String> response = restTemplate.exchange(
"some/url/", HttpMethod.GET, new HttpEntity<Object>(requestHeaders),
String.class);它自动解码gzip,因此不再需要手动解码。
https://stackoverflow.com/questions/28265973
复制相似问题