You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot 2集成Lettuce连接Redis失败:类找不到异常求助

解决Spring Boot 2 + Spring Data Redis + Lettuce的ClassNotFoundException问题

这个错误java.lang.ClassNotFoundException: io.lettuce.core.KeyValue本质是Lettuce客户端依赖缺失或者版本与Spring Boot版本不兼容导致的,下面是几个针对性的解决步骤:

1. 确保Spring Boot依赖管理正确引入

如果用Maven,先确认pom.xml里引入了Spring Boot父依赖——它会自动管理Lettuce的适配版本,从根源避免版本冲突:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>你的Spring Boot 2.x版本号</version>
    <relativePath/> <!-- 从仓库查找父依赖 -->
</parent>

然后直接引入Spring Data Redis的starter,它会自动包含适配好的Lettuce驱动:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

⚠️ 注意:除非你明确知道版本适配规则,否则不要手动指定Lettuce的版本,Spring Boot父依赖已经帮你做好了版本匹配。

2. 排除冲突的Redis驱动(如果存在)

如果项目中不小心引入了Jedis驱动,可能会和Lettuce产生依赖冲突,需要手动排除Jedis:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 清理并重新构建项目

有时候依赖下载不完整或者本地缓存有问题,执行以下操作强制更新:

  • Maven:运行mvn clean install -U
  • Gradle:运行gradle clean build --refresh-dependencies

4. 手动指定Lettuce版本(仅当必要时)

如果确实需要手动指定Lettuce版本,要严格匹配Spring Boot 2的版本:

  • Spring Boot 2.0.x → Lettuce 5.0.x
  • Spring Boot 2.1.x → Lettuce 5.1.x
  • Spring Boot 2.2.x → Lettuce 5.2.x
  • Spring Boot 2.3+ → Lettuce 5.3+

举个例子,若使用Spring Boot 2.3.10.RELEASE,手动引入Lettuce的代码如下:

<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>5.3.7.RELEASE</version>
</dependency>

5. 检查IDE依赖加载状态

有时候IDE的依赖缓存会出错,你可以:

  • 右键项目 → 选择Maven/Gradle → Reload Projects
  • 打开IDE的依赖库面板,确认lettuce-core已存在且版本正确

补充:io.lettuce.core.KeyValue是Lettuce 5.x版本新增的类,如果项目里引入的是Lettuce 4.x,就会出现找不到类的情况。而Spring Boot 2.x默认适配Lettuce 5.x,所以大概率是依赖引入错误或版本冲突导致的问题。

内容的提问来源于stack exchange,提问作者Chirdeep Tomar

火山引擎 最新活动