版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yingziisme/article/details/94591067
实际使用遇到的问题 – 获取ResponseBody不完整和Memory LEAK的问题
具体的代码如下
@Slf4j
@Component
public class ResponseBodyFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange.mutate().response(responseDecorator(exchange)).build());
}
private ServerHttpResponseDecorator responseDecorator(ServerWebExchange exchange) {
return new ServerHttpResponseDecorator(exchange.getResponse()) {
ServerHttpResponse serverHttpResponse = exchange.getResponse();
DataBufferFactory bufferFactory = serverHttpResponse.bufferFactory();
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
MediaType mediaType = exchange.getResponse().getHeaders().getContentType();
if (null == mediaType || (!mediaType.includes(MediaType.APPLICATION_JSON) && !mediaType.includes(MediaType.APPLICATION_JSON_UTF8))) {
log.info("===not support for the mediaType : {}", mediaType);
return super.writeWith(body);
}
if (body instanceof Flux) {
Flux<? extends DataBuffer> flux = (Flux<? extends DataBuffer>) body;
return super.writeWith(flux.map(databuffer -> {
byte[] bytes = new byte[databuffer.readableByteCount()];
databuffer.read(bytes);
DataBufferUtils.release(databuffer);
String responseBody = new String(bytes, StandardCharsets.UTF_8);
log.info("responseBody: \n{}", responseBody);
return bufferFactory.wrap(bytes);
}));
}
return super.writeWith(body);
}
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return writeWith(Flux.from(body).flatMapSequential(p -> p));
}
};
}
@Override
public int getOrder() {
return -4;
}
}
发起一个请求,可以看到响应分成了两次打印
2019-05-28 23:55:38.023 INFO 1580 --- [ctor-http-nio-7] c.m.d.gateway.filter.ResponseBodyFilter : responseBody:
{"aee":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","de":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","cee":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","be":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","bee":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqw
2019-05-28 23:55:38.761 INFO 1580 --- [ctor-http-nio-7] c.m.d.gateway.filter.ResponseBodyFilter : responseBody:
ertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","bccccccccc":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopb","eee":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","dee":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","aaaaaaa":"aeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","beee":"beqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","deee":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopd","eeesde":"eeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuioeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbn","deesdfe":"deqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","edddddddd":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ee":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","a":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","b":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ce":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","c":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","d":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ae":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","e":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","aeee":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","beedsfe":"beqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ceee":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","eeee":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiope","dddddddd":"deqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","asdfeee":"aeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","cdddddd":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ceesde":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop"}
@Slf4j
@Component
public class ResponseBodyFilter1 implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange.mutate().response(responseDecorator(exchange)).build());
}
private ServerHttpResponseDecorator responseDecorator(ServerWebExchange exchange) {
return new ServerHttpResponseDecorator(exchange.getResponse()) {
ServerHttpResponse serverHttpResponse = exchange.getResponse();
DataBufferFactory bufferFactory = serverHttpResponse.bufferFactory();
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
MediaType mediaType = exchange.getResponse().getHeaders().getContentType();
if (null == mediaType && (mediaType.includes(MediaType.APPLICATION_JSON) || mediaType.includes(MediaType.APPLICATION_JSON_UTF8))) {
log.info("===not support for the mediaType", mediaType);
return super.writeWith(body);
}
return DataBufferUtils.join(Flux.from(body))
.flatMap(dataBuffer -> {
DataBufferUtils.retain(dataBuffer);
final Flux<DataBuffer> cachedFlux = Flux.defer(() -> Flux.just(dataBuffer.slice(0, dataBuffer.readableByteCount())));
BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromDataBuffers(cachedFlux);
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, exchange.getResponse().getHeaders());
int lenght = dataBuffer.readableByteCount();
byte[] content = new byte[lenght];
dataBuffer.read(content);
String bodyString = "";
if (!ObjectUtils.isEmpty(exchange.getResponse().getHeaders().get(HttpHeaders.CONTENT_ENCODING))
&& exchange.getResponse().getHeaders().get(HttpHeaders.CONTENT_ENCODING).contains("gzip")) {
GZIPInputStream gzipInputStream = null;
try {
gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(content), lenght);
StringWriter writer = new StringWriter();
IOUtils.copy(gzipInputStream, writer, StandardCharsets.UTF_8);
bodyString = writer.toString();
} catch (IOException e) {
log.error("====Gzip IO error", e);
} finally {
if (gzipInputStream != null) {
try {
gzipInputStream.close();
} catch (IOException e) {
log.error("===Gzip IO close error", e);
}
}
}
} else {
bodyString = new String(content, StandardCharsets.UTF_8);
}
log.info("bodyString: {}", bodyString);
return Mono.defer(() -> bodyInserter.insert(outputMessage, new BodyInserterContext()))
.then(Mono.defer(() -> {
Flux<DataBuffer> messageBody = Flux.just(bufferFactory.wrap(content));
HttpHeaders httpHeaders = getDelegate().getHeaders();
if (!httpHeaders.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
messageBody = messageBody.doOnNext(data -> httpHeaders.setContentLength(data.readableByteCount()));
}
return getDelegate().writeWith(messageBody);
}));
});
}
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return writeWith(Flux.from(body).flatMapSequential(p -> p));
}
};
}
@Override
public int getOrder() {
return -4;
}
}
发起同样一个请求
会看到只有一次完整的body的打印
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* 2019-05-28 23:58:22.120 INFO 12280 --- [ctor-http-nio-7] c.m.d.g.filter.ResponseBodyFilter1 : bodyString: {"aee":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","de":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","cee":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","be":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","bee":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","bccccccccc":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopb","eee":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","dee":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","aaaaaaa":"aeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","beee":"beqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","deee":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopd","eeesde":"eeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuioeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbn","deesdfe":"deqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","edddddddd":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ee":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","a":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","b":"bbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ce":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","c":"cbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","d":"dbeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ae":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","e":"ebeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","aeee":"abeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","beedsfe":"beqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ceee":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","eeee":"eqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiope","dddddddd":"deqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","asdfeee":"aeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","cdddddd":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop","ceesde":"ceqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiopeqwertyuiopasdfghjklzxcvbnm,asdfghjklqwertyuiop"}
*/
代码如下
@Slf4j
@Component
public class ResponseBodyFilter2 implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange.mutate().response(responseDecorator(exchange)).build());
}
private ServerHttpResponseDecorator responseDecorator(ServerWebExchange exchange) {
return new ServerHttpResponseDecorator(exchange.getResponse()) {
ServerHttpResponse serverHttpResponse = exchange.getResponse();
DataBufferFactory bufferFactory = serverHttpResponse.bufferFactory();
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return super.writeWith(DataBufferUtils.join(Flux.from(body))
.map(dataBuffer -> {
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
DataBufferUtils.release(dataBuffer);
return content;
}).flatMap(bytes -> {
MediaType mediaType = serverHttpResponse.getHeaders().getContentType();
if (null == mediaType || (!mediaType.includes(MediaType.APPLICATION_JSON) && !mediaType.includes(MediaType.APPLICATION_JSON_UTF8))) {
} else {
String bodyString = "";
int length = bytes.length;
if (!ObjectUtils.isEmpty(exchange.getResponse().getHeaders().get(HttpHeaders.CONTENT_ENCODING))
&& exchange.getResponse().getHeaders().get(HttpHeaders.CONTENT_ENCODING).contains("gzip")) {
GZIPInputStream gzipInputStream = null;
try {
gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes), length);
StringWriter writer = new StringWriter();
IOUtils.copy(gzipInputStream, writer, StandardCharsets.UTF_8);
bodyString = writer.toString();
} catch (IOException e) {
log.error("====Gzip IO error", e);
} finally {
if (gzipInputStream != null) {
try {
gzipInputStream.close();
} catch (IOException e) {
log.error("===Gzip IO close error", e);
}
}
}
} else {
bodyString = new String(bytes, StandardCharsets.UTF_8);
}
log.info("bodyString: {}", bodyString);
}
return Mono.just(bufferFactory.wrap(bytes));
}));
}
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return writeWith(Flux.from(body).flatMapSequential(p -> p));
}
};
}
@Override
public int getOrder() {
return -4;
}
}