RedisTemplate.opsForValue().set redis赋值实体对象时出现异常。

异常信息:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: com.example.sringredis.Models.Book] with root cause

异常代码:controller类

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("/test1")
    public void test1(){
        ValueOperations<String,String> ops1=stringRedisTemplate.opsForValue();
        ops1.set("name","三国演戏");
        String name=ops1.get("name");
        System.out.println(name);
        ValueOperations ops2=redisTemplate.opsForValue();
        Book bk1=new Book();
        bk1.setId(1);
        bk1.setName("红楼梦");
        bk1.setPrice(12.3f);
        bk1.setAuthor("曹雪芹");
        ops2.set("bk1",bk1);
        Book bk2=(Book) ops2.get("bk1");
        System.out.println(bk2);
    }

Book实体类:

public class Book  {
    private Integer id;
    private String name;
    private String author;
    private Float price;
}

解决方案:book实体类,继承 Serializable

修改后的book实体类:

public class Book implements Serializable {
    private Integer id;
    private String name;
    private String author;
    private Float price;
}

运行成功

参考网址:org.springframework.data.redis.serializer.SerializationException: Cannot serialize(解决redis存入对象序列化)_at org.springframework.data.redis.serializer.strin-CSDN博客

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值