gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:00 25 4
gpt4 key购买 nike

我是 Spring Cloud Gateway 的新手(spring boot 2.0.5.RELEASE)。我尝试从网络过滤器读取请求正文,但请求被卡住,无法流经链。示例代码:

@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() 行,一切都会正常。有什么线索吗?谢谢!

最佳答案

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

这句话解释得非常准确:

The biggest problem is that I find out that HttpServletRequest(get from NativeWebRequest) cannot read input stream(some parameters are in the request body) more than one time

关于java - Spring Cloud Gateway - 尝试读取 Web 过滤器中的请求正文时请求卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com