专栏首页13blog.siteorg.springframework.data.redis.serializer.SerializationException: Cannot serialize;

org.springframework.data.redis.serializer.SerializationException: Cannot serialize;

前言

本文中提到的解决方案,源码地址在:perfect-ssm,希望可以帮你解决问题。

问题描述

在Spring与Redis整合过程中,出现了如下报错:

    org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is                 org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested         exception is java.io.NotSerializableException: com.ssm.promotion.core.entity.Article

    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:92)
    at org.springframework.data.redis.cache.RedisCache$CacheValueAccessor.convertToBytesIfNecessary(RedisCache.java:375)
    at org.springframework.data.redis.cache.RedisCache$BinaryRedisCacheElement.<init>(RedisCache.java:408)
    at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:173)
    at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:157)
    at com.ssm.promotion.core.redis.RedisUtil.put(RedisUtil.java:31)
    at test.RedisTest.redisPutListArticleTest(RedisTest.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: com.ssm.promotion.core.entity.Article
    at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:68)
    at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:35)
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:90)
    ... 34 more
Caused by: java.io.NotSerializableException: com.ssm.promotion.core.entity.Article
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at java.util.ArrayList.writeObject(ArrayList.java:762)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:46)
    at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:63)
    ... 36 more

解决方案

通过异常栈可以得出有效信息:

Caused by: java.io.NotSerializableException: com.ssm.promotion.core.entity.Article

NotSerializable即为不可序列化的对象,对象Article不可序列化因此出现了此问题。

要缓存的JavaBean必须实现Serializable接口,因为Spring会将对象先序列化再存入 Redis

针对于此异常,修改对应的JavaBean对象即可,实现Serializable

public class Article implements Serializable

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)

    测试又报bug啦 接到测试小伙伴的问题,说是一个接口不返回数据了,好吧,虽然不是我写的接口任务落到头上也得解决,本地调试了一下,好家伙,直接抛了个异常出来,这又...

    我是十三
  • org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service()

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题。 至于为什么已经写了一篇文章thymeleaf模板引擎调...

    我是十三
  • org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题。 至于为什么已经写了一篇文章thymeleaf模板引擎调...

    我是十三
  • com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1146177 > 1048576).

    版权声明:本文为博主原创文章,欢迎转载。 ...

    程裕强
  • Spring Cloud Config Server迁移节点或容器化带来的问题

    为了说明下面的内容,我们可以先尝试重现一下问题:在一个测试环境中,将Spring Cloud Config的配置中心迁移到另外一个节点上,即配置中心的IP地址发...

    程序猿DD
  • mybatis框架org.apache.ibatis.exceptions.PersistenceException:

    沁溪源
  • SpringMvc上传文件抛出3次Max

    SpringMvc 3.2.18 版本开发的文件上传在Tomcat7 上运行抛出了3个MaxUploadSizeExceededException 异常; 正...

    py3study
  • 一条SQL引发的惨案!已解决!

    这本是昨天的异常,后来没腾出时间记录下来,本就是一条平平常常的SQL,同事遇到的bug,最后解决也算精彩。

    疯狂的KK
  • java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

    1、启动项目报出这个错误,未找到响应的包,所以需要你将包从远程仓库下载到本地仓库即可。

    别先生
  • Hibernate 5 在保存数据的时候提示数据库没有选择

    09:06:40.277 [http-nio-8080-exec-1] ERROR o.h.id.enhanced.TableStructure - could...

    HoneyMoose

扫码关注云+社区

领取腾讯云代金券