SerializationException异常产生原因及解决方案
SerializationException异常产生原因及解决方案
01 异常发生场景
- 当我试图取出我存入redis的数据时
@Test
void contextLoads() {
ValueOperations valueOperations=redisTemplate.opsForValue();
valueOperations.append("n2","666");
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方法视为添加语句
-
而正确的语句是
valueOperations.set("n2","666");
-
这个SerializationException算是比常见的问题了,简单来说就是无法从redis中get到数据,我犯的错是没有存,redis里没有数据当然取不到
03 解决方式
@Test
void contextLoads() {
ValueOperations valueOperations=redisTemplate.opsForValue();
valueOperations.set("n2","666");
System.out.println(valueOperations.get("n2"));
}
- 当我正确存入数据后自然就成功了