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


完整报错信息:

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的组件的中加入排除旧版jedis的依赖

<exclusions>
    <exclusion>
        <groupId>redis.clientsgroupId>
        <artifactId>jedisartifactId>
    exclusion>
exclusions>

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

原因2:

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

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

原因2的解决方法:

在pom.xml的中加入以下配置:

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-pool2artifactId>
    <version>2.11.1version>
dependency>

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