springboot集成reids缓存,使用分页时报java.lang.ClassCastException

1.之前已经使用redis作为缓存,对单个对象和List对象做缓存时均没有问题。

   redis本身没有出现问题,当然配置也没有问题。序列化和反序列化也没出问题

2.新增了一个分页器,手写的分页对象,对分页查询的数据缓存

    一开报错:

DefaultSerializer requires a Serializable payload but received an object of type [com.penghf.springboot_mybatis.util.PageUtil]

    这是我忘记给新增的分页对象PageUtil实现序列化接口Serializable,加上

    再查询,报错:

java.lang.ClassCastException: com.penghf.springboot_mybatis.pojo.Girl cannot be cast to com.penghf.springboot_mybatis.pojo.Girl

    怎么还会报类型转化错误了,关键是自己转化成自己错了????

    去redis查数据,数据已经存进去了,那就是取出来的问题了。

    DEBUG一试:

    流程走下去,看起来都没问题,没办法上网查:

    说是devtools导致的,原来是我再项目之初就是用了devtools进行快速部署(所谓的热部署)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    删掉删掉!!!

   重启,好了!搞定了!

重点原因:

启动日志:

使用了devtools

2019-03-05 10:23:50.554  INFO 6792 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-05 10:23:50.557  INFO 6792 --- [  restartedMain] c.p.s.SpringbootMybatisApplication       : Started SpringbootMybatisApplication in 3.22 seconds (JVM running for 4.071)
2019-03-05 10:23:50.559 DEBUG 6792 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Creating new Restarter for thread Thread[main,5,main]
2019-03-05 10:23:50.560 DEBUG 6792 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Immediately restarting application
2019-03-05 10:23:50.560 DEBUG 6792 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@ef26b1
2019-03-05 10:23:50.560 DEBUG 6792 --- [  restartedMain] o.s.boot.devtools.restart.Restarter      : Starting application com.penghf.springboot_mybatis.SpringbootMybatisApplication with URLs [file:/E:/Workspaces/workspace-career/springboot_mybatis/target/classes/]

不适用devtools

2019-03-05 10:22:04.651  INFO 12836 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-05 10:22:04.653  INFO 12836 --- [           main] c.p.s.SpringbootMybatisApplication       : Started SpringbootMybatisApplication in 2.792 seconds (JVM running for 3.477)

当对象被序列化到缓存里时,当前应用的类加载器是C1,当你改变了一些代码或者配置文件的时候,DevTools 工具将会自动重新启动这个容器,并且创建一个新的类加载器 C2. 这时候调用这个具有缓存的方法时,缓存管理将会从缓存里找到该条缓存记录并进行反序列化操作。如果缓存库不考虑上下文的话,也就是没注意到类加载器的变化时,该对象将会有错误的类加载器

其实就是因上下文类加载器不同而产生这样的错误,那么归根结底就是因SpringBoot DevTools工具搞的

猜你喜欢

转载自blog.csdn.net/huofuman960209/article/details/88169379
今日推荐