spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

org.springframework.core.io.buffer.DefaultDataBuffer cannot be cast to org.springframework.core.io.buffer.NettyDataBuffer #3356

Closed zhuyunkai-allen closed 6 months ago

zhuyunkai-allen commented 6 months ago

springcloud version:Hoxton.SR9 my code:

private ServerHttpResponseDecorator serverHttpResponseDecorator(ServerHttpResponse originalResponse) {
    DataBufferFactory dataBufferFactory = originalResponse.bufferFactory();
    return new ServerHttpResponseDecorator(originalResponse) {
    @Override
        public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
            if (body instanceof Flux) {
                return super.writeWith(((Flux<? extends DataBuffer>)body).buffer().map(dataBuffers -> {
                    DataBuffer join = dataBufferFactory.join(dataBuffers);
                    byte[] byteArray = new byte[join.readableByteCount()];
                    join.read(byteArray);
                    DataBufferUtils.release(join);
                    String originalResponseBody = new String(byteArray, StandardCharsets.UTF_8);
                    // 加密
                    byte[] encryptedByteArray =
                        encryptResponse(originalResponseBody).getBytes(StandardCharsets.UTF_8);
                    originalResponse.getHeaders().setContentLength(encryptedByteArray.length);
                    originalResponse.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
                    return dataBufferFactory.wrap(encryptedByteArray);
                }));
                                }
            return super.writeWith(body);
        }
    };
}

When I was encrypting the return results at the gateway, the following error occurred:

[ ERROR] [2024-04-16 10:21:20.208] org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler [122] [reactor-http-nio-5]- [26802c54]  500 Server Error for HTTP POST "/file-service/api/file/minIO/upload"
java.lang.ClassCastException: org.springframework.core.io.buffer.DefaultDataBuffer cannot be cast to org.springframework.core.io.buffer.NettyDataBuffer
    at org.springframework.cloud.gateway.filter.NettyRoutingFilter.lambda$null$2(NettyRoutingFilter.java:136)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP POST "/file-service/api/file/minIO/upload" [ExceptionHandlingWebHandler]
Stack trace:
        at org.springframework.cloud.gateway.filter.NettyRoutingFilter.lambda$null$2(NettyRoutingFilter.java:136)
        at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.poll(FluxMapFuseable.java:177)
        at reactor.netty.channel.MonoSendMany$SendManyInner.run(MonoSendMany.java:259)
        at reactor.netty.channel.MonoSendMany$SendManyInner.trySchedule(MonoSendMany.java:381)
        at reactor.netty.channel.MonoSendMany$SendManyInner.onSubscribe(MonoSendMany.java:212)
        at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)
        at reactor.core.publisher.FluxJust.subscribe(FluxJust.java:70)
        at reactor.core.publisher.Flux.subscribe(Flux.java:8143)
        at reactor.netty.channel.MonoSendMany.subscribe(MonoSendMany.java:82)
        at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:153)
        at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
        at reactor.core.publisher.Mono.subscribe(Mono.java:4105)
        at reactor.netty.NettyOutbound.subscribe(NettyOutbound.java:329)
        at reactor.core.publisher.MonoSource.subscribe(MonoSource.java:58)
        at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
        at reactor.netty.http.client.HttpClientConnect$HttpIOHandlerObserver.onStateChange(HttpClientConnect.java:430)
        at reactor.netty.ReactorNetty$CompositeConnectionObserver.onStateChange(ReactorNetty.java:494)
        at reactor.netty.resources.PooledConnectionProvider$DisposableAcquire.onStateChange(PooledConnectionProvider.java:526)
        at reactor.netty.resources.PooledConnectionProvider$PooledConnection.onStateChange(PooledConnectionProvider.java:435)
        at reactor.netty.channel.ChannelOperationsHandler.channelActive(ChannelOperationsHandler.java:60)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:225)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:211)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:204)
        at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelActive(CombinedChannelDuplexHandler.java:414)
        at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:69)
        at io.netty.channel.CombinedChannelDuplexHandler.channelActive(CombinedChannelDuplexHandler.java:213)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:225)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:211)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:204)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelActive(DefaultChannelPipeline.java:1410)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:225)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:211)
        at io.netty.channel.DefaultChannelPipeline.fireChannelActive(DefaultChannelPipeline.java:907)
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:305)
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:335)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:688)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:635)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:552)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:514)
        at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1050)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)
zhuyunkai-allen commented 6 months ago

When my request header is application/json, the code executes normally, but when the request header is multipart/form-data, the code throws an error.

spencergibb commented 6 months ago

Hoxton is no longer supported. Is it still an issue with 2023.0.1?

zhuyunkai-allen commented 6 months ago

My jdk version is 8,i don't know 2023.0.1 is ok

spencergibb commented 6 months ago

No, spring boot 3.x requires java 17

spring-cloud-issues commented 6 months ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues commented 6 months ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.