当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot从redis取缓存发生java.lang.ClassCastException异常

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

2018年09月24日  | 移动技术网IT编程  | 我要评论

目录树

  • 异常日志信息
  • 错误原因
  • 解决方法

 

异常日志信息

  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>

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网