Spring Cloud Gateway-尝试在Web过滤器中读取请求正文时请求被卡住

时间:2019-01-11 09:47:40

标签: java spring spring-boot spring-cloud-gateway

我是Spring Cloud Gateway的新手(Spring Boot 2.0.5.RELEASE)。我尝试从Web过滤器读取请求正文,并且请求只是卡住了,无法流过整个链。示例代码:

@Component
public class TestFilter implements GlobalFilter, Ordered {

    private static final Logger logger = LoggerFactory.getLogger(TestFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        ServerHttpRequest serverHttpRequest = exchange.getRequest();
        try {
            /* whenever I put the following line. The request cannot get through */
            ByteBuffer byteBuffer = Mono.from(serverHttpRequest.getBody()).toFuture().get().asByteBuffer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return chain.filter(exchange);
    }
}

如果我删除getBody()行,则一切正常。有什么线索吗?谢谢!

1 个答案:

答案 0 :(得分:0)

在这里看看:How can I read request body multiple times in Spring 'HandlerMethodArgumentResolver'?

此评论非常准确:

  

最大的问题是,我发现HttpServletRequest(从NativeWebRequest获取)不能多次读取输入流(某些参数在请求正文中)