当前位置: 首页 > 工具软件 > Web.Java > 使用案例 >

java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHand

凌和悦
2023-12-01

楔子

使用ssm,HandlerInterceptorAdapter 时,获取执行的方法,出现如下错误

java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod
    at cn.zhuzi.kindle.interceptor.PrivilegeInterceptorAdapter.preHandle(PrivilegeInterceptorAdapter.java:38)
    at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:133)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:962)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

出错事例


public class PrivilegeInterceptorAdapter extends HandlerInterceptorAdapter {
    @Resource
    JdbcTemplate jdbcTemplate;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object preHandle) throws Exception {

        System.out.println(request.getRequestURI());
        /* if (preHandle instanceof HandlerMethod) { */
        //此处出现上述错误。使用了
        HandlerMethod method = (HandlerMethod) preHandle;
        Prev pre = method.getMethod().getAnnotation(Prev.class);
        if (pre == null) {
            return true;
        } else {
            String oprator = pre.oprator();
            String decription = pre.decription();
            System.out.println("操作权限-->" + decription);

            // TODO模拟 从session获取权限
            @SuppressWarnings("unchecked")
            List<String> privilegeList = (List<String>) request.getSession().getAttribute("privilege");

            if (privilegeList.contains(oprator)) {
                return true;
            } else {
                if (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {// 是ajax请求
                    response.setCharacterEncoding("text/html;charset=UTF-8");
                    response.setContentType("text/html;charset=UTF-8");
                    PrintWriter writer = response.getWriter();
                    StringBuffer jsonTip = new StringBuffer("{\"result\":");
                    jsonTip.append(false).append(",\"tip\":\"您无权操作\"}");
                    writer.print(jsonTip.toString());
                    return false;
                } else {// 非ajax请求
                    String content = request.getSession().getServletContext().getContextPath();
                    // response.sendRedirect(content +
                    // "/refuse.html");//转发到页面
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("您无权操作");
                    return false;
                }
            }

        }
    }
-- 使用   System.out.println(request.getRequestURI()); 打印请求地址时 
发现,只有在请求资源找不到的情况下才会出现上述转换错误。

解决
1 加上  if (preHandle instanceof HandlerMethod) { 做判断
2 不处理,因为请求的资源不存在
 类似资料:

相关阅读

相关文章

相关问答