JDK 序列化, 碰到serialVersionUID 不一致问题,怎么处理?
阅读本文大概需要 2.8 分钟。
来自:https://juejin.cn/post/6961229793056686117
serialVersionUID
。serialVersionUID
,但是由于服务较多,上线发版时会有一段新老版本共存的时期,所以得考虑这些对象序列化的兼容问题,新的对象反序列化一定得兼容老的对象Java Object Serialization
java.io.Serializable
接口,基本使用方式如下:Serialization
// Serialize today's date to a file.
FileOutputStream f = new FileOutputStream("tmp");
ObjectOutput s = new ObjectOutputStream(f);
s.writeObject("Today");
s.writeObject(new Date());
s.flush();
Deserialization
// Deserialize a string and date from a file.
FileInputStream in = new FileInputStream("tmp");
ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();
serialVersionUID
private static final long serialVersionUID = 1L;
InvalidClassException
异常serialVersionUID(long)
,这个 ID 的计算规则是通过当前类信息(类名、属性等)去生成的,所以当属性有变更时这个serialVersionUID 也一定会发生变更serialVersionUID 不一致时的兼容处理
ObjectInputStream
来实现,那么这里自定义一个 CompatibleInputStream
继承ObjectInputStream
,然后重写 readClassDescriptor
方法即可public class CompatibleInputStream extends ObjectInputStream {
private static Logger logger = LoggerFactory.getLogger(CompatibleInputStream.class);
public CompatibleInputStream(InputStream in) throws IOException {
super(in);
}
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass resultClassDescriptor = super.readClassDescriptor(); // initially streams descriptor
Class localClass; // the class in the local JVM that this descriptor represents.
try {
localClass = Class.forName(resultClassDescriptor.getName());
} catch (ClassNotFoundException e) {
logger.error("No local class for " + resultClassDescriptor.getName(), e);
return resultClassDescriptor;
}
ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
if (localClassDescriptor != null) { // only if class implements serializable
final long localSUID = localClassDescriptor.getSerialVersionUID();
final long streamSUID = resultClassDescriptor.getSerialVersionUID();
if (streamSUID != localSUID) { // check for serialVersionUID mismatch.
final StringBuffer s = new StringBuffer("Overriding serialized class version mismatch: ");
s.append("local serialVersionUID = ").append(localSUID);
s.append(" stream serialVersionUID = ").append(streamSUID);
Exception e = new InvalidClassException(s.toString());
logger.error("Potentially Fatal Deserialization Operation.", e);
resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization
}
}
return resultClassDescriptor;
}
}
https://stackoverflow.com/a/1816711/6507948
// Deserialize a string and date from a file.
FileInputStream in = new FileInputStream("tmp");
//反序列化时使用上面的CompatibleInputStream即可
ObjectInputStream s = new CompatibleInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();
推荐阅读:
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅