Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 868
bright Avatar asked Oct 15 '25 13:10

bright


1 Answers

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(); 
    // ...
}
like image 100
sp00m Avatar answered Oct 18 '25 13:10

sp00m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!