发现网上查到的解决方案并不可行,所以提供了一个亲测可行的方案。
解决思路:
········在反序列化过程中,serialVersionUID版本号校验之前,利用反射,将目标实例的serialVersionUID强制设置为本地类的serialVersionUID,以此跳过两者版本号不一致的问题。
实现代码如下:
import java.io.*;
import java.lang.reflect.Field;
public class SerializeUtil {
private SerializeUtil() {}
public static Object toObject (byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
CompatibleInputStream compatibleInputStream = new CompatibleInputStream(byteArrayInputStream);
return compatibleInputStream.readObject();
}
private static class CompatibleInputStream extends ObjectInputStream {
public CompatibleInputStream(InputStream in) throws IOException {
super(in);
}
@Override
protected ObjectStreamClass readClassDescripto