//兼容请求mock接口结果返回gzip数据,但是request请求头没有添加Accept-Encoding:gzip,deflate,br
private void gzipBugfix(CloseableHttpResponse zuulResponse) throws IOException {
Header firstHeader = zuulResponse.getFirstHeader(ZuulHeaders.CONTENT_ENCODING);
if (Objects.nonNull(firstHeader)) {
String contentEnc = firstHeader.getValue();
if (!StringUtils.isEmpty(contentEnc)
&& HTTPRequestUtils.getInstance().isGzipped(contentEnc)
&& !RequestContext.getCurrentContext().isGzipRequested()) {
//响应为gzip但是请求不是gzip,解压转换为String
try (InputStream ins = zuulResponse.getEntity() == null ? null : zuulResponse.getEntity().getContent();) {
if (Objects.nonNull(ins)) {
Header type = zuulResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);
zuulResponse.setEntity(new StringEntity(
StreamUtils.copyToString(new GZIPInputStream(ins), StandardCharsets.UTF_8),
Objects.isNull(type) ? ContentType.APPLICATION_JSON : ContentType.parse(type.getValue())));
}
zuulResponse.removeHeaders(ZuulHeaders.CONTENT_ENCODING);
}
}
}
}
Zuul中gzip处理
最新推荐文章于 2024-04-23 16:34:46 发布