利用拦截器获取方法名
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
if(handler instanceof HandlerMethod) {
Object bean = ((HandlerMethod) handler).getBean();
Class<?> beanType = ((HandlerMethod) handler).getBeanType();
Method method = ((HandlerMethod) handler).getMethod();
System.out.println("****当前的类:"+bean.getClass().toString()+"*******当前的方法:"+method.getName());
RequestMapping methodAnnotation = ((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class);
String[] value = methodAnnotation.value();
System.out.println(value.toString());
}
其中handler参数想强转成上面代码所示,但报错了,因为我们的handler其实是个controller实体,且handler instanceof HandlerMethod=false!!!但是有的同事是不报错的,我这边是报错的,不知道为什么!
本人的解决方案是在spring-mv