我正在尝试解压缩从 REST 服务收到的 gzip:ed 响应:
Content-Encoding=[gzip], Content-Type=[application/json], Content-Length=[710] ...
我正在使用 Grails REST Client Builder 插件:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept "application/json"
}
返回的响应是 Spring ResponseEntity .我正在尝试使用 GZIPInputStream
解压缩数据:
String body = response.getBody()
new GZIPInputStream(new ByteArrayInputStream(body.getBytes())).text
这对 Caused by ZipException: Not in GZIP format
显然我做错了什么,但我不知道是什么。所有建议都适用。
最佳答案
如果您真的需要继续使用 Rest Client Builder,您只需稍微修改您的客户端代码:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept byte[].class, "application/json" }
请注意 accept 调用中的额外参数 - byte[].class - 表示 RestTemplate 应避免对响应进行任何解析。
要解压缩,您现在可以执行以下操作:
new GZIPInputStream(new ByteArrayInputStream(response.body))
是的,我知道,已经接受了回答,但有些人可能仍然觉得它在无法切换 Rest 组件的情况下很有用。
关于grails - 使用 GZIPInputStream 解压缩 REST 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265973/