简体   繁体   中英

Spring mvc interceptor exception

My project is baded on spring mvc, and I wrote a interceptor to intercept request, I want to get parametrts from request, the follows is my code:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    HandlerMethod maControl = (HandlerMethod) handler;  
    Method pmrResolver = (Method) maControl.getMethod();  
    String methodName = pmrResolver.getName(); 
        ....
}

but now it throws a exception:

java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to  org.springframework.web.method.HandlerMethod

What is the cause of the Exception?

It simply means that handler isn't an instance of HandlerMethod , so the cast fails. Check before casting as follow:

if (handler instanceof HandlerMethod) {
    HandlerMethod maControl = (HandlerMethod) handler;  
    Method pmrResolver = (Method) maControl.getMethod();  
    String methodName = pmrResolver.getName(); 
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM