springboot中的redisTemplate拆箱可能会产生空指针异常。

huangapple go评论97阅读模式
英文:

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 = &quot;requestWrapperFilter&quot;, urlPatterns = {&quot;/*&quot;})
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&lt;String, Serializable&gt; redisTemplate(LettuceConnectionFactory connectionFactory) {
		RedisTemplate&lt;String, Serializable&gt; redisTemplate = new RedisTemplate&lt;&gt;();
		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 &#39;redisTemplate.hasKey(COMMON_HEAD_TOKEN_NAME + token)&#39; may produce &#39;NullPointerException&#39; 
Unchecked call to &#39;hasKey(K)&#39; as a member of raw type &#39;org.springframework.data.redis.core.RedisTemplate&#39; 

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&lt;String, Serializable&gt; redisTemplate;

huangapple
  • 本文由 发表于 2020年10月3日 17:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64182596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定