3

我的项目是在spring mvc上做的,我写了一个拦截器来拦截请求,我想从请求中获取参数,下面是我的代码:

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

但现在它抛出了一个异常:

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

异常的原因是什么?

4

1 回答 1

3

它只是意味着 thathandler不是 的实例HandlerMethod,因此强制转换失败。铸造前检查如下:

if (handler instanceof HandlerMethod) {
    HandlerMethod maControl = (HandlerMethod) handler;  
    Method pmrResolver = (Method) maControl.getMethod();  
    String methodName = pmrResolver.getName(); 
    // ...
}
于 2013-08-28T06:59:29.813 回答