set redis hello get redis
问题:命令行set数据,用代码读取不到数据,同样代码写入数据命令行 get不到数据。
命令行监控:monitor
,代码执行读取,看到 redis 上打印内容:
"\"\xe4\xbd\xa0\xe5\xa5\xbd,redis 2023-03-05:15:23:07.429\"" # java 代码会将 key 转化成对应的十六进制的数值进行操作
解决:
配置redisTemplate的序列化操作
@Configuration @EnableCaching //开启注解 public class RedisConfig extends CachingConfigurerSupport { /** * retemplate相关配置 * 序列化配置,如果没有配置序列化的话可能会出现往redis存了值,但是获取不到 */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); template.setValueSerializer(jacksonSeial); template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); return template; } }