Spring会话在我的数据库中存储序列化对象。问题是,有时我的代码会改变。有时我的物品会改变。这很正常。但是,我会遇到如下错误:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [byte[]] to type [java.lang.Object] for value '{-84, ..., 112}'; 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.mysite.MyClass; local class incompatible: stream classdesc serialVersionUID = 1432849980928799324, local class serialVersionUID = 8454085305026634675
我通过调用带有HttpSession的Spring引导端点作为参数来获得此错误,例如:
@GetMapping("/stuff")
public @ResponseBody MyClass getStuff(HttpSession session) {
try {
Object myObject = session.getAttribute("MyClass");
if (myObject != null && myObject instanceof MyClass) {
return (MyClass) myObject;
} else {
return null;
}
} catch (Exception e) {
logger.warn("Invalid session data", e);
return null;
}
}
但是,由于抛出了异常
之前
方法被调用时,我无法从这个正常的预期错误中恢复。
作为一种解决方法,每次部署时,我都必须删除整个会话表,尽管大多数对象仍然兼容!
明确地说,解决方案不是添加SerialVersionUID。因为从一个部署到下一个部署,对象确实会以不兼容的方式发生变化。这不是一个序列化问题。这是一个Spring会话错误恢复问题。
我的问题是:我怎样才能优雅地从这些问题中恢复过来?