当前位置:
首页 >
行业知识 >
org.springframework.data.redis.serializer.SerializationException: Cannot serialize(解决redis存入对象序列化)
行业知识
org.springframework.data.redis.serializer.SerializationException: Cannot serialize(解决redis存入对象序列化)
2026-12-04 21:26:17
来源:互联网转载
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.InvalidClassException: com.xs.entity.XXX; class invalid for deserializationat org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize
在调用service 实现类时报出以上错误,原因是因为 spring会先将对象序列化,再存入redis进行缓存,而entity没有实现序列化接口,因此序列化出错,需要在对应的实体类中添加序列化即可(implements Serializable),如下:
()@Override@Cacheable(value = "student")public Student getStudentById(int id) { return studentMapper.getStudentById(id);} public class Student implements Serializable{ private int id; private String name; private String fullname;} ()