从WebFlux中的WebFilter获取HandlerMethod
当使用Servlet API实现拦截器时,我HandlerMethod开箱即用:
... extends HandlerInterceptorAdapter@Override
public boolean preHandle(final HttpServletRequest request,
final HttpServletResponse response, final Object handlerMethod) throws Exception {
我可以HandlerMethod在实施时访问而WebFilter不是HandlerInterceptorAdapter吗?
如果WebFilter我有:
... implements WebFilter {public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
一旦我可以HandlerMethod通过调用进行访问serverWebExchange.getAttribute("....bestMatchingHandler"),但是它不再起作用。参见相应的问题。我的问题是:HandlerMethod不使用serverWebExchange.getAttribute怎么办?
回答:
我找到了答案,这也有助于回答我原来的问题。HandlerMethod可以这样得到:
(HandlerMethod) this.handlerMapping.getHandler(serverWebExchange).toProcessor().peek();这里handlerMapping是一个类型的豆RequestMappingHandlerMapping,你可以从WebFlux注入。
以上是 从WebFlux中的WebFilter获取HandlerMethod 的全部内容, 来源链接: utcz.com/qa/434893.html
