SpringCloudGateway笔记(7)-响应不全

本文记录了在使用SpringCloudGateway时遇到的响应不全和内存泄漏问题,详细介绍了三种不同的处理方式及其效果,包括导致ResponseBody不完整的问题及引发Memory LEAK的解决方案,并提供了可行的代码示例和GitHub资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实际使用遇到的问题 – 获取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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值