SpringBoot访问Redis报错java.lang.NoSuchMethodError: redis.clients.jedis.JedisPoolConfig.setMinEvictableIdleTime

完整报错信息:

Servlet.service()for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed;
nested exception is java.lang.NoSuchMethodError: redis.clients.jedis.JedisPoolConfig.setMinEvictableIdleTime(Ljava/time/Duration;)V] with root cause

java.lang.NoSuchMethodError: redis.clients.jedis.JedisPoolConfig.setMinEvictableIdleTime(Ljava/time/Duration;)V

 

原因 1:

Maven 中有其它组件依赖了旧版的 jedis,需要排除

 

原因 1 的解决方法:

在使用了旧版 jedis 的组件的 <dependency></dependency> 中加入排除旧版 jedis 的依赖

<exclusions>
    <exclusion>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </exclusion>
</exclusions>

重新加载 Maven 的依赖组件,重新运行即可。

 

原因 2:

JedisPoolConfig 和 JedisSentinelPool 使用了 apache.commons 的 commons-pool2 包

虽然编译代码时不会报错,但运行时会提示找不到包。(怀疑是用反射使用的原因)

 

原因 2 的解决方法:

在 pom.xml 的 <dependencies></dependencies> 中加入以下配置:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>

重新加载 Maven 的依赖组件,重新运行即可。