从WebFlux中的WebFilter获取HandlerMethod [英] Get HandlerMethod from WebFilter in WebFlux

查看:956
本文介绍了从WebFlux中的WebFilter获取HandlerMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆