问题描述
- 数据存入到redis后查看发现是乱码,无法正常识别
- value序列化方式使用不当,导致出现错误“java.lang.ClassCastException: org.apache.shiro.session.mgt.SimpleSession cannot be cast to java.lang.String”
解决方法
需要指定key和value的序列化方式
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(new JdkSerializationRedisSerializer());
//value hashmap序列化
template.setHashValueSerializer(redisSerializer);
//key haspmap序列化
template.setHashKeySerializer(redisSerializer);
return template;
}