栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

spring boot从redis取缓存发生java.lang.ClassCastException异常

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot从redis取缓存发生java.lang.ClassCastException异常

异常日志信息

  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热部署注释掉:


    org.springframework.boot
    spring-boot-devtools
    true

 

原文出处:https://www.cnblogs.com/wind-june/p/9695707.html  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号