首页 > 编程语言 > 详细

SpringBoot java.lang.ClassCastException:ResourceHttpRequestHandler cannot be cast to HandlerMethod

时间:2021-07-30 22:57:23      阅读:28      评论:0      收藏:0      [点我收藏+]

Springboot 在配置个性化的webMVC时,静态资源的请求也被拦截的时候,会出现以上错误,所以需要忽略对静态资源的拦截

1) @Configuration
public class MyMvcConfig implements WebMvcConfigurer {

// 拦截所有请求
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new AuthenticationInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/*","/js/*","/images/*","/swagger-ui.html","/**/*.html","/**/*.css","/**/*.js");
}
}

以上代码,
excludePathPatterns  "/**/*.html","/**/*.css","/**/*.js"部分就是忽略静态资源请求的配置

2) 并在HandlerInterceptor的实现类里面判断如果时静态资源类的请求的话,直接放过通行
public class AuthenticationInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
if((handler instanceof HandlerMethod)){// 如果不是method,直接放行
return true;
}
if(handler instanceof ResourceHttpRequestHandler){
return true;
}
}

SpringBoot java.lang.ClassCastException:ResourceHttpRequestHandler cannot be cast to HandlerMethod

原文:https://www.cnblogs.com/learning520/p/15081166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!