1. 在使用spring-cloud-gateway当作网关服务,当收到post请求,且content-type为application/x-www-form-urlencoded时,网关服务报如下异常:
2018-10-08 13:11:34.971 ERROR 8304 --- [ctor-http-nio-3] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [POST http://127.0.0.1:8088/demo/login]

java.lang.IllegalStateException: Only one connection receive subscriber allowed.
	at reactor.ipc.netty.channel.FluxReceive.startReceiver(FluxReceive.java:279) ~[reactor-netty-0.7.9.RELEASE.jar:0.7.9.RELEASE]
	at reactor.ipc.netty.channel.FluxReceive.lambda$subscribe$2(FluxReceive.java:129) ~[reactor-netty-0.7.9.RELEASE.jar:0.7.9.RELEASE]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446) ~[netty-transport-4.1.29.Final.jar:4.1.29.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
	at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_60]

该异常大概意思是连接中的请求体只能被订阅一次,也就是说这个请求的body已经被读取过一次了,然后我从网上查阅各种资料也请教了大神,提供的解决思路都是通过缓存body之后,再重新封装一次request转发出去,但是我试过N多遍,并且各种改良,我的接口服务始终接收不到任何参数。无奈,后来在stackoverflow发布了这个问题,得到了国外大神的指点有两个解决办法

 

方法一:

    其实这个问题是在spring-boot-2.0.5这个版本上才出现的,所以将版本改为2.0.4即可,如果不想改版本号可参考方法二。

 

方法二:

    使用如下代码禁用HiddenHttpMethodFilter 过滤器,该过滤器的作用请参考:https://blog.csdn.net/geloin/article/details/7444321

    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
                return chain.filter(exchange);
            }
        };
    }

 

国外大神关于这个问题的讨论帖:https://github.com/spring-cloud/spring-cloud-gateway/issues/541

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐