Get HandlerMethod from WebFilter in WebFlux

耗尽温柔 提交于 2019-12-31 05:03:31

问题


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 {

Can I get access to HandlerMethod while implementing WebFilter instead of HandlerInterceptorAdapter?

In case of WebFilter I have:

... implements WebFilter {
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {

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?


回答1:


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();

where handlerMapping is a bean of type RequestMappingHandlerMapping which you can inject from WebFlux.



来源:https://stackoverflow.com/questions/51840647/get-handlermethod-from-webfilter-in-webflux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!