grails – 使用GZIPInputStream解压缩REST响应
发布时间:2020-12-14 16:20:54 所属栏目:大数据 来源:网络整理
导读:我正在尝试解压缩从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
我正在尝试解压缩从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 这不能由ZipException引起:不是GZIP格式 显然有些事情我做错了,但我无法弄清楚是什么.所有建议都是适当的. 解决方法
如果您确实需要继续使用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组件不是一个选项. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |