前言
获取SpringCloud gateway 网关响应的response的值 乱码,postman调试发现是由gzip导致,把postman中的header值 Accept-Encoding 勾掉就不乱码了
获取SpringCloud gateway 响应的response的值 乱码
gzip
POSTMAN工具请求里的gzip压缩头导致获取响应值一直乱码,解决gzip压缩后响应值获取正常
/*解码 gzip*/
private byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
log.error("gzip uncompress error.", e);
}
return out.toByteArray();
}
/*编码 gzip*/
private byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch (IOException e) {
log.error("gzip compress error.", e);
}
return out.toByteArray();
}
private Result<Object> dealResult(String originalBody, Object parseObject) {
Result<Object> result;
if (HttpStatus.OK.equals(this.getStatusCode())) {
result = Result.buildSuccessResult(parseObject);
GatewayLogUtil.printInfoLog(exchange, originalBody, log);
} else {
result = Result.buildErrorResult(parseObject);
GatewayLogUtil.printErrorLog(exchange, originalBody, log);
}
return result;
}