爬坑系列----Redis查询key报空指针异常,而redis中确实存在该key

现象:

  1、在A方法中根据key查询一个list,可以获取到相应的值

  2、在B方法中同样调用此方法,传入相同的key,查询不到值,为null,报空指针异常

起初我也一脸懵逼,到现在虽然解决了,还是不知道根本原因

分析:

  1、key肯定是相同的,redis也有该key,  A能查出,B查不出。那就肯定是哪里不一样。

  2、后来分析有可能是在查redis的时候,redis内部存在不同。

  3、这个时候就觉得是序列化的问题

  4、查看redis连接配置

@Autowired
private RedisConfigProperties redis;
@Bean
public RedisSerializer fastJson2JsonRedisSerializer() { ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
return new FastJson2JsonRedisSerializer<Object>(Object.class);
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大空闲接连
jedisPoolConfig.setMaxIdle(redis.getMaxIdle());
//最小空闲连接
jedisPoolConfig.setMinIdle(redis.getMinIdle());
//连接池最大阻塞等待时间
jedisPoolConfig.setMaxWaitMillis(redis.getMaxWait());
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
//主机地址
jedisConnectionFactory.setHostName(redis.getHost())
//端口
jedisConnectionFactory.setPort(redis.getPort());
//密码
jedisConnectionFactory.setPassword(redis.getPassword());
//索引
jedisConnectionFactory.setDatabase(redis.getDatabase());
//超时时间
jedisConnectionFactory.setTimeout(redis.getTimeOut());
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory, RedisSerializer fastJson2JsonRedisSerializer) {
StringRedisTemplate redisTemplate = new StringRedisTemplate(factory); redisTemplate.setConnectionFactory(redisConnectionFactory());
//redis 开启事务
redisTemplate.setEnableTransactionSupport(true);
//hash 使用jdk 的序列化
redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
//StringRedisSerializer key 序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//keySerializer 对key的默认序列化器。默认值是StringSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
// valueSerializer
redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}

  5、标黄部分,使用过各种序列化工具,都失败。

  6、突然突发奇想,会不会是redis事务的问题,然后  redisTemplate.setEnableTransactionSupport(true); 将事务关闭,莫名其妙就可以了。

  7、有哪位知道根本原因,请留言告知,万分感谢

  8、最后的配置如下

  

@Bean
public RedisSerializer fastJsonRedisSerializer(){
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
return new FastJsonRedisSerializer<Object>(Object.class);
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大空闲接连
jedisPoolConfig.setMaxIdle(redis.getMaxIdle());
//最小空闲连接
jedisPoolConfig.setMinIdle(redis.getMinIdle());
//连接池最大阻塞等待时间
jedisPoolConfig.setMaxWaitMillis(redis.getMaxWait());
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
//主机地址
jedisConnectionFactory.setHostName(redis.getHost());
//端口
jedisConnectionFactory.setPort(redis.getPort());
//密码
jedisConnectionFactory.setPassword(redis.getPassword());
//索引
jedisConnectionFactory.setDatabase(redis.getDatabase());
//超时时间
jedisConnectionFactory.setTimeout(redis.getTimeOut());
jedisConnectionFactory.setUsePool(true);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
return jedisConnectionFactory;
}
 @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory,RedisSerializer fastJsonRedisSerializer) {
StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
redisTemplate.setConnectionFactory(redisConnectionFactory());
//redis 开启事务
// redisTemplate.setEnableTransactionSupport(true); //hash 使用jdk 的序列化
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// ObjectMapper om = new ObjectMapper();
// om.setVisibility( PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);/*new JdkSerializationRedisSerializer()*/
//StringRedisSerializer key 序列化//一般可以不做设置
//redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//keySerializer 对key的默认序列化器。默认值是StringSerializer
//redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
上一篇:axios常用操作


下一篇:CSS 加载新方式