从WebFlux中的WebFilter获取HandlerMethod [英] Get HandlerMethod from WebFilter in WebFlux
问题描述
当使用Servlet API实现拦截器时,我开箱即用HandlerMethod
:
When implementing interceptor with Servlet API I got HandlerMethod
out of the box:
... extends HandlerInterceptorAdapter
@Override
public boolean preHandle(final HttpServletRequest request,
final HttpServletResponse response, final Object handlerMethod) throws Exception {
在实现WebFilter
而不是HandlerInterceptorAdapter
时可以访问HandlerMethod
吗?
Can I get access to HandlerMethod
while implementing WebFilter
instead of HandlerInterceptorAdapter
?
如果是WebFilter
,我有:
... implements WebFilter {
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
一旦我可以通过调用serverWebExchange.getAttribute("....bestMatchingHandler")
访问HandlerMethod
,但是它不再起作用.请参阅相应问题.我的问题是:如何在不使用serverWebExchange.getAttribute
的情况下获取HandlerMethod
?
Once I could access HandlerMethod
by invoking serverWebExchange.getAttribute("....bestMatchingHandler")
, but it doesn't work anymore. See corresponding question. My question here is: how can I get HandlerMethod
without using serverWebExchange.getAttribute
?
推荐答案
我找到了答案,这也有助于回答我的原始问题. HandlerMethod
可以这样获得:
I found the answer which helped to answer also my original question. HandlerMethod
can be got this way:
(HandlerMethod) this.handlerMapping.getHandler(serverWebExchange).toProcessor().peek();
其中handlerMapping
是类型为RequestMappingHandlerMapping
的bean,您可以从WebFlux注入它.
where handlerMapping
is a bean of type RequestMappingHandlerMapping
which you can inject from WebFlux.
这篇关于从WebFlux中的WebFilter获取HandlerMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!