idea对下边的代码做出了一个提示:
redisTemplate.hasKey(key)
提示说 Unboxing of redisTemplate.hasKey(key)may produce NullPointerException.
网上搜了下,参考了博文Unboxing of ‘stringRedisTemplate.hasKey(xx)‘ may produce ‘NullPointerException‘ 警告_初级码农-CSDN博客
先看下hasKey的代码:
public Boolean hasKey(K key) {
byte[] rawKey = this.rawKey(key);
return (Boolean)this.execute((connection) -> {
return connection.exists(rawKey);
}, true);
}
对connection.exists的返回结果做了封装。再看看exist的代码:
@Nullable
default Boolean exists(byte[] key) {
Assert.notNull(key, "Key must not be null!");
Long count = this.exists(key);
return count != null ? count > 0L : null;
}
这个返回结果可能是null。因此出现了最开始的问题。
所以把代码改成:
Objects.equals(redisTemplate.hasKey(key)),Boolean.TRUE)