在Spring-mvc拦截器中,如何访问处理程序控制器方法?
问题内容:
在Spring-mvc拦截器中,我想访问处理程序控制器方法
public class CustomInterceptor implements HandlerInterceptor {
public boolean preHandle(
HttpServletRequest request,HttpServletResponse response,
Object handler) {
log.info(handler.getClass().getName()); //access to the controller class
//I want to have the controller method
...
return true;
}
...
}
问题答案:
您可以将转换Object handler
为HandlerMethod
。
HandlerMethod method = (HandlerMethod) handler;
但是请注意,handler
传递给的参数preHandle
并不总是a
HandlerMethod
(请注意ClassCastException
)。HandlerMethod
然后具有可用于获取注释等的方法。