SerializationException异常产生原因及解决方案

桃扇骨 2024-02-17 09:30 201阅读 0赞

SerializationException异常产生原因及解决方案

01 异常发生场景

  • 当我试图取出我存入redis的数据时

    @Test
    void contextLoads() {

    1. ValueOperations valueOperations=redisTemplate.opsForValue();
    2. valueOperations.append("n2","666");
    3. System.out.println(valueOperations.get("n2"));

    }
    //org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.StreamCorruptedException:

02 异常的产生原因

  • 通过查询语句,发现在数据库中没有出现对应的key

    keys *

  • 原来是我打错函数把append方法视为添加语句

  • 而正确的语句是

    1. valueOperations.set("n2","666");
  • 这个SerializationException算是比常见的问题了,简单来说就是无法从redis中get到数据,我犯的错是没有存,redis里没有数据当然取不到

03 解决方式

  1. @Test
  2. void contextLoads() {
  3. ValueOperations valueOperations=redisTemplate.opsForValue();
  4. valueOperations.set("n2","666");
  5. System.out.println(valueOperations.get("n2"));
  6. }
  • 当我正确存入数据后自然就成功了

发表评论

表情:
评论列表 (有 0 条评论,201人围观)

还没有评论,来说两句吧...

相关阅读