英文:
springboot Unboxing of redisTemplate. may produce NullPointerException
问题
我在项目中使用了Spring Boot(2.3.1)和Lettuce。
过滤器
@Slf4j
@WebFilter(filterName = "requestWrapperFilter", urlPatterns = {"/*"})
public class RequestWrapperFilter implements Filter {
    @Resource
    private RedisTemplate redisTemplate;
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // ...
        if (redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token)) {
            redisTemplate.delete(COMMON_HEAD_TOKEN_NAME + token);
        }
        // ...
    }
    // ...
}
Redis配置如下:
@Configuration
@Component
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}
但是当我调用接口时,应用程序显示以下错误:
有一个警告:
对 'redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token)' 的取消装箱可能会产生 'NullPointerException'
对原始类型 'org.springframework.data.redis.core.RedisTemplate' 的成员 'hasKey(K)' 的未经检查的调用
我可以忽略它吗?
英文:
I use springboot(2.3.1) and Lettuce in project
Filter
@Slf4j
@WebFilter(filterName = "requestWrapperFilter", urlPatterns = {"/*"})
public class RequestWrapperFilter implements Filter {
	@Resource
	private RedisTemplate redisTemplate;
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	...
				if (redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token)) {
					redisTemplate.delete(COMMON_HEAD_TOKEN_NAME + token);
				}
	}
	...
}
the RedisConfig configration like this:
@Configuration
@Component
public class RedisConfig {
	@Bean
	public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
		RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
		redisTemplate.setConnectionFactory(connectionFactory);
		return redisTemplate;
	}
}
but when I calling interface , the application shows the following error:
there is WARNING:
Unboxing of 'redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token)' may produce 'NullPointerException' 
Unchecked call to 'hasKey(K)' as a member of raw type 'org.springframework.data.redis.core.RedisTemplate' 
can I ignore ?
答案1
得分: 4
第一个警告意味着 hasKey 方法返回一个对象包装器,但在 if 条件语句内部使用它会隐式地对其进行拆箱(即将调用结果转换为原始值)。如果由于某种原因 hasKey 方法返回了 null,将会出现错误。为了安全起见,请按以下方式检查键的存在:
if (Boolean.TRUE.equals(redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token))) {
第二个警告意味着你的 redisTemplate 字段具有原始类型,然而 RedisTemplate 类是参数化的。为了消除警告,请在过滤器中如下定义 redisTemplate 字段:
@Resource
private RedisTemplate<String, Serializable> redisTemplate;
英文:
The first warning means that the hasKey method returns an object wrapper, but using it inside an if condition implicitly unboxes it (i.e. converts the result of the call to a primitive value). If for some reason the hasKey method returns null, you'll get an error. To be on the safe side, check the presence of the key as follows:
if (Boolean.TRUE.equals(redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token))) {
The second warning means that your redisTemplate field has a raw type, however, the RedisTemplate class is parameterized. To get rid of the warning, define the redisTemplate field in the filter as follows:
@Resource
private RedisTemplate<String, Serializable> redisTemplate;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论