解决springboot2.x报错:io.lettuce.core.RedisAsyncCommandsImpl cannot be cast to redis.clients.jedis.Jedis

这篇博客介绍了如何处理SpringBoot2应用中遇到的将Lettuce转换为Jedis的错误。作者指出,由于SpringBoot2默认使用Lettuce作为Redis客户端,因此在尝试使用Jedis进行某些操作时会出现类型转换错误。解决方案是手动在pom.xml文件中排除Lettuce依赖并引入Jedis。博主提供了具体的代码示例和配置修改步骤,帮助读者解决问题。
摘要由CSDN通过智能技术生成

解决springboot2报错:io.lettuce.core.RedisAsyncCommandsImpl cannot be cast to redis.clients.jedis.Jedis的问题

源代码:

  redisTemplate.opsForValue().set("int_key", "1");
        stringRedisTemplate.opsForValue().set("int", "1");
        //+1运算
        stringRedisTemplate.opsForValue().increment("int");

        //-1运算,stringRedisTemplate耐不活
        //所以,通过StringRedisTemplate获取底层Jedis连接
        Jedis jedis = (Jedis) stringRedisTemplate.
                getConnectionFactory().
                getConnection().
                getNativeConnection();
        
        //-1运算
        jedis.decr("int");

        Map<String, String> hash = new HashMap<>();

        hash.put("file1","value1");
        hash.put("file2","value2");

        stringRedisTemplate.opsForHash().putAll("hash",hash);

        BoundHashOperations<String, Object, Object> hashOps =
                stringRedisTemplate.boundHashOps("hash");
        hashOps.delete("file1","file2");
        hashOps.put("file4","value5");

        Map<String ,Object> map = new HashMap<>();
        map.put("success",true);
        return map;

重点就是出错在:

		//-1运算,stringRedisTemplate耐不活
        //所以,通过StringRedisTemplate获取底层Jedis连接
        Jedis jedis = (Jedis) stringRedisTemplate.
                getConnectionFactory().
                getConnection().
                getNativeConnection();

因为springboot2.x中默认使用了Lettuce集成了Redis服务,
所以spring-boot-starter-data-redis中默认只引入了Lettuce,而没有引入Jedis

所以需要我们在pom文件中手动引入**Jedis**,并且去除redis中的Lettuce

pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YxYL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值