spring-boot-devtools cannot be cast to类型转换异常java.lang.ClassCastException
TIP
在使用 DevTools 时,通用Mapper经常会出现 class x.x.A cannot be cast to x.x.A。 同一个类如果使用了不同的类加载器,就会产生这样的错误,所以解决方案就是让通用Mapper和实体类使用相同的类加载器即可。 DevTools 默认会对 IDE 中引入的所有项目使用 restart 类加载器,对于引入的 jar 包使用 base 类加载器,因此只要保证通用Mapper的jar包使用 restart 类加载器即可。
基本原因是因为classLoader不同造成的,如果使用了spring-dev-tools之后,就会使用spring自己的RestartClassLoader
来装载类
错误堆栈:
java.lang.ClassCastException: com.zh.car.core.utils.DataGrid cannot be cast to com.zh.car.core.utils.DataGrid
at com.alibaba.dubbo.common.bytecode.proxy3.getDataGridByModel(proxy3.java) ~[dubbo-2.6.0.jar:2.6.0]
at com.zh.car.admin.controller.TestController.index(TestController.java:41) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]</pre><p>网上大部分是推荐不使用spring-devtools工具,但是这样相当于没解决这个错误。</p><p><br/></p><p>官方文章:<a href="https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html" target="_blank" textvalue="https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html" rel="noopener">https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html</a> </p><p>官方给出的教程是创建属性文件</p><p><span style="color: rgb(109, 24, 11); font-family: Consolas, "liberation mono", Courier, monospace; background-color: rgb(242, 242, 242);">META-INF/spring-devtools.properties</span></p><p>然后在属性文件中添加:</p><pre class="brush:bash;toolbar:false">restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\w-]+\.jar
java.lang.ClassCastException: com.zh.car.core.utils.DataGrid cannot be cast to com.zh.car.core.utils.DataGrid
at com.alibaba.dubbo.common.bytecode.proxy3.getDataGridByModel(proxy3.java) ~[dubbo-2.6.0.jar:2.6.0]
at com.zh.car.admin.controller.TestController.index(TestController.java:41) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]</pre><p>网上大部分是推荐不使用spring-devtools工具,但是这样相当于没解决这个错误。</p><p><br/></p><p>官方文章:<a href="https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html" target="_blank" textvalue="https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html" rel="noopener">https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html</a> </p><p>官方给出的教程是创建属性文件</p><p><span style="color: rgb(109, 24, 11); font-family: Consolas, "liberation mono", Courier, monospace; background-color: rgb(242, 242, 242);">META-INF/spring-devtools.properties</span></p><p>然后在属性文件中添加:</p><pre class="brush:bash;toolbar:false">restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\w-]+\.jar
这里就是告诉spring,哪一些jar用RestartClassLoader 来装载类
因为项目是配置的多模块,结构:
xxx-parent
-xxx-api
-xxx-admin
-xxx-service
-xxx-common
api->service,中间的RPC是用的dubbo,就是controller调用dubbo的service,出现了类型转换错误。
在api项目的resources中添加META_INF/spring-devtools.properties文件
把dubbo的jar加进来
restart.include.dubbo=/dubbo-[\w-\.]+jar
如果在项目中用到了不是resource的类就需要把jar包含进来。
重新启动项目,然后在修改类,自动重启成功,调用不报错。成功!
参考链接
https://github.com/teaey/spring-boot-starter-dubbo/issues/214