当前位置: 代码迷 >> 综合 >> springboot 整合redis 遇到取出对象为空
  详细解决方案

springboot 整合redis 遇到取出对象为空

热度:81   发布时间:2023-10-22 12:09:38.0

场景:我是出现在多系统对同一个redis取值,简单点就是A系统存了upi对象,B系统取upi对象。取不到了返回空。当时是一点错都没有心中一万个草拟吗

解决方案:代码实例化redisTemplent的时候讲序列化类型改成存储时的序列化类型。

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

没错我们的两个系统架构不一样,系统从传统的springmvc向springboot走。

springboot是好东西,好到觉得它是万能的。实际是各种坑搞的我所以差点放弃他,其实还是自己没吃透。

犯错过程:我们原有项目的redisTemplate是这样配置的,没错用的是spring.data.redis的stringRedisSerializer

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /><!-- 防止出现序列值 --><property name="keySerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="valueSerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="hashKeySerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="hashValueSerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property></bean>

最后整体解释一下:这个问题其实和springboot和redis没有太大关系,主要是负责序列化和反序列化的东东的是一个。没错就是pring.data.redis的stringRedisSerializer ,顺便说一句不配置的话默认用的是java自己的java.io.Serializable 所以懂了吧