bright
bright

Reputation: 189

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?

Upvotes: 4

Views: 5720

Answers (1)

sp00m
sp00m

Reputation: 48817

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(); 
    // ...
}

Upvotes: 3

Related Questions