code> classes) 如果新版本不串行兼容,那么你需要用自定义 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 protectedstatic 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 = -3344057582987646196I'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本地类不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 13:36
查看更多