2016-12-01 10 views
0

ハンドラーメソッドにWebフィルターのリフレクションで何とかアクセスし、コントロールをディスパッチャーサーブレットに渡さずにrequestPattern値を取得する必要があります。これどうやってするの? 私はそのためにHttpServletRequestオブジェクトを持っていて、なんとなく@AutowireApplicationContextというオブジェクトもあります。指定されたHttpServletRequestオブジェクトのハンドラースプリングコントローラークラスとハンドラーメソッドを知る方法

ありがとうございました。

答えて

1

私は、DispatcherServlet自体がどのようなハンドラメソッドを決定してそのような方法でコントロールを与え、実装するのか見始めました。 コードは次のとおりです。

//Initialization in filter constructor 
.... 
final HandlerMapping handlerMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, HandlerMapping.class, true, false).get("requestMappingHandlerMapping"); 
.... 

@Override 
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { 
     ..... 
     Method mappingMethod = null; 
     try { 
      mappingMethod = ((HandlerMethod)handlerMappings.getHandler(request).getHandler()).getMethod(); 
      RequestMapping requestMapping = mappingMethod.getAnnotation(RequestMapping.class); 
      final String requestPattern = requestMapping.value(); 
     } 
     catch(Exception ex){ 
      logger.error("Error getting the mapping bean for the request URL " + request.getRequestURI(), ex); 
      return; 
     } 
     .... 
    }