开发者社区> 问答> 正文

类型转换出错 400 请求报错 

类型转换出错 将DefaultServletHttpRequestHandler 转换为HandlerMethod类型

org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod
如何将DefaultServletHttpRequestHandler 转换为HandlerMethod类型

 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String businessName = resolve(urlPathHelper.getServletPath(request));         request.setAttribute(TENANT_BUSINESS_NAME_KEY, businessName);         // restrict the access         HandlerMethod  method = (HandlerMethod) handler;         TenantResource tenantResource = method.getMethodAnnotation(TenantResource.class);         RootResource rootResource = method.getMethodAnnotation(RootResource.class);         boolean isRootResource = false;         // get annotation from class when no annotation is specified         if (tenantResource == null && rootResource == null) {             tenantResource = AnnotationUtils.findAnnotation(method.getBeanType(), TenantResource.class);             rootResource = AnnotationUtils.findAnnotation(method.getBeanType(), RootResource.class);         }

        // still with no annotation, set default         if (tenantResource == null && rootResource == null) {             isRootResource = true;         }

        // tenant resource         if (tenantResource != null && StringUtils.isEmpty(businessName)) {             throw new NoHandlerFoundException(request.getMethod(), request.getRequestURI(), null);         }

        // root resource         if ((rootResource != null || isRootResource) && !StringUtils.isEmpty(businessName)) {             throw new NoHandlerFoundException(request.getMethod(), request.getRequestURI(), null);         }
        return super.preHandle(request, response, handler);     } 加红的那句出错了,求大神帮助

展开
收起
kun坤 2020-05-30 15:16:54 456 0
1 条回答
写回答
取消 提交回答
  • 我今天也遇到这个问题了,你的请求里面还带有其他的请求的时候,而且不是你配置的拦截的规则,那么它转换类型的时候就报错了。我这边是页面请求里面还有js的请求,所以controller的方法请求通过了,js的拦截到的时候,你的handler 其实不是你配置的那个,所以就出现这个问题了
    解决办法:
    if (handler instanceof HandlerMethod) { permission = ((HandlerMethod) handler).getMethodAnnotation(Permission.class); } else { return true; }

    2020-05-30 15:17:00
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载