springboot集成redis存对象乱码如何解决(redis,springboot,开发技术)

时间:2024-04-29 05:45:08 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    springboot%E9%9B%86%E6%88%90redis%E5%AD%98%E5%AF%B9%E8%B1%A1%E4%B9%B1%E7%A0%81%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3

其实本质上不算是一种乱码,只是序列化之后存储的东西而已,当我们使用RedisTemplete存储对象,时,如果该对象没有被序列化则会报错,序列化之后得到的不是自己想要的数据,为了解决这种方法,我们可以使用 StringRedisTemplete

默认使用redistemplete时

当我们运行时会发现报错了!提示我们User对象没有序列化。

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.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.xxx.redis01.model.User]

.....
Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.xxx.redis01.model.User]
at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43)
at org.springframework.core.serializer.Serializer.serializeToByteArray(Serializer.java:56)
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:60)
... 72 more


Process finished with exit code -1

当我们给User对象序列化之后运行

天啦噜,竟然不乱码了!!! ,但是–当你使用linux 远程连接后查看keys 时你会发现,欸? 这里乱码了。。。

2022-04-01 20:32:07.666 INFO 18640 --- [ main] com.xxx.redis01.RedisListTest01 : No active profile set, falling back to 1 default profile: "default"
2022-04-01 20:32:07.998 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-04-01 20:32:08.000 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-04-01 20:32:08.018 INFO 18640 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 Redis repository interfaces.
2022-04-01 20:32:08.752 INFO 18640 --- [ main] com.xxx.redis01.RedisListTest01 : Started RedisListTest01 in 1.488 seconds (JVM running for 2.517)
User{id=2, name='lisi', age='19'}

Process finished with exit code 0

这是什么鬼? 明明是user1 这是啥啊

解决方式肯定不是只有一种,只是自己学习记录,,仅供参考

成功拿到

我们在linux下看看,同样ok

当然还有另一种方式,存入JSON格式,这里就不放代码了.

本文:springboot集成redis存对象乱码如何解决的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:使用pd.merge表连接出现多余行如何解决下一篇:

14 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18