Java序列化 - java.io.InvalidClassException本地类不兼容 [英] Java serialization - java.io.InvalidClassException local class incompatible

查看:230
本文介绍了Java序列化 - java.io.InvalidClassException本地类不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现Serializable的公共类,它由多个其他类扩展。之前只有那些子类被序列化过 - 从来都不是超类。



超类定义了一个serialVersionUID。



我不确定它是否重要,但它没有标记为私有,而是它只是默认保护 - 你可能会说它是受包保护的

  static final long serialVersionUID = -7588980448693010399L; 

超类,也没有任何子类,但是实现了readObject或writeObject,没有子类有一个明确定义的serialVersionUID。我认为超类中定义的一个就足够了。



尽管如此,只要读回以前的序列化对象,直到新的实例变量List / ArrayList和一个新方法被添加到超类中,并且一些私有实例变量被添加到其子类之一。



现在尝试回读之前的序列化对象,抛出异常。一个类似于:

  com.SomeCompany.SomeSubClass;本地类不兼容:stream classdesc serialVersionUID = 1597316331807173261,local class serialVersionUID = -3344057582987646196 

我假设这个是因为我没有在任何子类中声明一个而使用的默认serialVersionUID因为超类和一个子类中的更改而发生了更改。



<关于如何摆脱这种困境的建议将不胜感激。我假设我需要实现readObject和writeObject,但除了调用defaultReadObject()和defaultWriteObject()之外,我不确定我需要做什么。我也不知道是否需要将serialVerisonUID添加到所有子类,或者是否需要每个子类实现readObject和writeObject,或者我是否可以实现它们一次,假设我需要在超类中完成。

解决方案

@DanielChapman 给出serialVersionUID的一个很好的解释,但没有解决方案。解决方案是:运行 所有类上的serialver 程序。将这些 serialVersionUID 值放在当前版本的类中。只要当前类与旧版本串行兼容,您应该没问题。 (请注意以后的代码:所有 Serializable code> classes)



如果新版本串行兼容,那么你需要用自定义<$做一些魔术c $ c> readObject 实现(如果您尝试编写 new 类数据,则只需要自定义 writeObject 这将与旧代码兼容)。一般来说,添加或删除类字段不会使类序列不兼容。通常会更改现有字段的类型。



当然,即使新类序列兼容,您仍可能需要自定义 readObject 实现。如果你想填写从旧版本的类中保存的数据中缺少的任何新字段,你可能想要这个(例如,你有一个新的List字段,你想在加载旧的类数据时初始化为空列表)。 / p>

I've got a public class, which implements Serializable, that is extended by multiple other classes. Only those subclasses were ever serialized before - never the super class.

The super class had defined a serialVersionUID.

I'm not sure if it matters, but it was not marked private, but rather it just had the default protection - you might say it was package protected

static final long serialVersionUID = -7588980448693010399L;

The super class, nor any of the subclasses, however implemented readObject or writeObject, and none of the subclasses had an explicitly defined serialVersionUID. I figured one defined in the superclass would be sufficient.

Despite all this, things were fine as far as reading back previously serialized objects until a new instance variable, a List/ArrayList, along with a new method was added to the super class, and some private instance variables were added to one of its subclasses.

Now when trying to read back previously serialized objects, an exception is being thrown. One similar to this:

com.SomeCompany.SomeSubClass; local class incompatible: stream classdesc serialVersionUID = 1597316331807173261, local class serialVersionUID = -3344057582987646196

I'm assuming this is caused because the default serialVersionUID, which was used because I didn't declare one in any of the subclasses, has now changed due to the changes in the superclass and one subclass.

Suggestions on how to get out of this dilemma would be appreciated. I'm assuming I need to implement readObject and writeObject, but other than invoking defaultReadObject() and defaultWriteObject(), I'm not exactly sure what I need to do. Nor do I know if I need to add serialVerisonUIDs to all of the subclasses or if readObject and writeObject need to be implemented by each subclass, or if I can just implement them once, assuming I need to at all, in the superclass.

解决方案

@DanielChapman gives a good explanation of serialVersionUID, but no solution. the solution is this: run the serialver program on all your old classes. put these serialVersionUID values in your current versions of the classes. as long as the current classes are serial compatible with the old versions, you should be fine. (note for future code: you should always have a serialVersionUID on all Serializable classes)

if the new versions are not serial compatible, then you need to do some magic with a custom readObject implementation (you would only need a custom writeObject if you were trying to write new class data which would be compatible with old code). generally speaking adding or removing class fields does not make a class serial incompatible. changing the type of existing fields usually will.

Of course, even if the new class is serial compatible, you may still want a custom readObject implementation. you may want this if you want to fill in any new fields which are missing from data saved from old versions of the class (e.g. you have a new List field which you want to initialize to an empty list when loading old class data).

这篇关于Java序列化 - java.io.InvalidClassException本地类不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆