实际使用遇到的问题 – 获取ResponseBody不完整和Memory LEAK的问题
第一种获取方式,会遇到获取ResponseBody不完整的问题
具体的代码如下
@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":"eqwertyuiopasdfghjklzxcvb