目錄樹
- 異常日志信息
- 錯誤原因
- 解決方法
異常日志信息
1 2018-09-24 15:26:03.406 ERROR 13704 --- [nio-8888-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.winds.admin.core.model.system.User cannot be cast to com.winds.admin.core.model.system.User] with root cause 2 3 java.lang.ClassCastException: com.winds.admin.core.model.system.User cannot be cast to com.winds.admin.core.model.system.User 4 at com.winds.admin.utils.web.WebValidateUtil.isLogin(WebValidateUtil.java:70) ~[classes/:na] 5 at com.winds.admin.web.controller.HelloController.login(HelloController.java:45) ~[classes/:na] 6 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_51] 7 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_51] 8 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_51] 9 at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_51] 10 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE] 11 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.19.RELEASE.jar:4.3.19.RELEASE] 12 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 13 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:849) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 14 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:760) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 15 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 16 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 17 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 18 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 19 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 20 at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.34.jar:8.5.34] 21 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.19.RELEASE.jar:4.3.19.RELEASE] 22 at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.34.jar:8.5.34] 23 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.34.jar:8.5.34] 24 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34] 25 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.34.jar:8.5.34] 26 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34] 27 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
錯誤原因
仔細看你會發現這個類轉換異常很奇怪,為什么呢?我們注意到這兩個User不管是包名還是類名是完全一樣的[ java.lang.ClassCastException: com.winds.admin.core.model.system.User cannot be cast to com.winds.admin.core.model.system.User ],但也發生了強制轉換異常,那么還有什么原因會引起這種情況呢?那就只有一種情況了:使用的類加載器不一樣。主要原因是pom文件中引入了DevTools配置。 當你使用DevTools進行緩存時,需要了解這一限制。 當對象序列化到緩存中時,應用程序類加載器是C1。然后,更改一些代碼或者配置后,devtools會自動重新啟動上下文並創建一個新的類加載器C2。所以當你通過redis操作獲取緩存反序列化的時候應用的類加載器是C2,雖然包名及其來類名完全一致,但是序列化與反序列化是通過不同的類加載器加載則在JVM中它們也不是同一個類。如果緩存庫沒有考慮上下文類加載器,那么這個對象會附加錯誤的類加載器 ,也就是我們常見的類強制轉換異常(ClassCastException)。
這個問題可參見:Stack OverFlow的文章。
解決方法
將devtools熱部署注釋掉:
<!-- 熱部署模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>