org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod


Spring boot 2.0 版本报错 ResourceHttpRequestHandler cannot be cast to HandlerMethod

在控制台的报错信息为:
java.lang.ClassCastException: org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod

其根本的原因就在于,Spring boot 2.0对静态资源也进行了拦截,当拦截器拦截到请求之后,但controller里并没有对应的请求时,该请求会被当成是对静态资源的请求。此时的handler就是 ResourceHttpRequestHandler,就会抛出以上的错误。

解决方案就是:在拦截器处将静态资源的请求路径进行排除
image
然后再将preHandler加上一层instanceof关键字进行判断
image


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM