java.io.InvalidClassException 异常解决, 实现Serializable接口的注意事项
解决方案: 在类中显式指定
private static final long serialVersionUID = 42L;
类实现序列化接口, 进行序列化反序列化的时候, 抛出 java.io.InvalidClassException 异常
java.io.InvalidClassException: com.xx.Xxx; local class incompatible: stream classdesc serialVersionUID = -783991920331, local class serialVersionUID = -331138183213
这个异常是由于反序列化时, 当前类的serialVersionUID 与 bytes中的类反序列化后的类的serialVersionUID 不同所致, 这个serialVersionUID 如果不在类中显式声明, 则是通过类名,方法名等诸多因素经过计算而得,理论上是一一映射的关系,也就是唯一的
JDK中Serializable接口的声明
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long: ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes
重要的几点:
1. 所有实现序列化的类, 都推荐显式声明序列化ID
2. 序列化ID的访问类型 推荐为 private, 因为只在自己内部被使用, 不会因为继承而流到子类
3. 数组是无法显示声明序列化ID的(比如String[], 你无法在其中声明serialVersionUID), 但是java的序列化也不会对数组对象进行serialVersionUID 的比较
java.io.InvalidClassException 异常解决, 实现Serializable接口的注意事项的更多相关文章
- java.io.NotSerializableException:异常解决
创建的pojo类未实现Serializable接口,启动服务器会报此异常. 解决此问题 添加一个Serializable接口即可 1 import java.io.Serializable; 2 3 ...
- spark 解决错误java.io.InvalidClassException
今天遇到一个现场问题,任务报错java.io.InvalidClassException.在开发环境是没有报错的,正式环境报错.大概类似于下面这样(非报错原文,摘自网上同类博客) java.io.In ...
- Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible
项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...
- 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法
今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...
- Java基础知识强化104:Serializable接口 与 Parcelable接口
1. 什么是 序列化 和 反序列化 ? 序列化 :序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间.序 ...
- java.io.WriteAbortedException异常
java.io.WriteAbortedException异常 未实现 public interface Serializable 接口的类将无法使其任何状态序列化或反序列化. 可序列化类的所有子类型 ...
- java.io.InvalidClassException
java.io.InvalidClassException public class PetroleumCoke implements Serializable { private static fi ...
- java.io.InvalidClassException: com.master.CurrentMessages; local class incompatible:
报错信息如下: java.io.InvalidClassException: com.master.CurrentMessages; local class incompatible: stream ...
- java.io.FileNotFoundException异常,一是“拒绝访问”,二是“系统找不到指定路径”
关于java.io.FileNotFoundException异常 因为这个异常抛出俩种情况:一是“拒绝访问”,二是“系统找不到指定路径” 这里只讲明什么时候抛拒绝访问,什么时候抛找不到指定路径. 原 ...
随机推荐
- js 正则匹配 域名【host】
如果直接在js中是可以直接取到hostname的,以下方式是通过正则匹配: var url = "http://www.cnblogs.com/cench" var reg = / ...
- Oracle集群和灾备解决方案介绍
Oracle本身有各种各样的解决方案,本文主要是对以下几种解决方案做一下简单的梳理. 1. Oracle Rac解决方案. Oracle Rac主要通过两台Oracle服务器来组件集群,提高Or ...
- An internal error occurred during: "J2EE Component Mapping Update".
1.错误描写叙述 An internal error occurred during: "J2EE Component Mapping Update". java.lang.Nul ...
- 智能路由器开发指南_book
最近购得一个openwrt书籍<智能路由器开发指南>,作者张永智. Building a smart router with openwrt 作者网址:http://openwrt.bjb ...
- 苹果mac shell 终端 命令行快捷键——行首行尾
ctrl+a //移到行首 ctrl+e //移到行尾 http://blog.csdn.net/hherima/article/details/47083739
- Java学习笔记之equals和Objects.equals
equals 相信大家就知道,就是比较,我们平时也会在自己定义的类中加入自己重写的equals用来比较两个类是否相同,例如这样 public class Person { private String ...
- iOS CLLocationManager定位
本文转载至 http://www.tuicool.com/articles/7JBRZn 在iOS8以前的版本中,我们使用CLLocationManager定位是没有问题的,最近在iOS8系统中却无法 ...
- C# RangeHelper
/// <summary> /// Range Helper /// </summary> /// <typeparam name="T">&l ...
- intellij idea pycharm phpstorm webstorm 这些编辑器像 sublime text 一样使用 ctrl d 来操作光标多选。
在Windows 和 Linux 快捷键是 Alt+J,在 Mac OS X 系统下快捷键是 Ctrl+G . 参考 https://blog.jetbrains.com/phpstorm/2014/ ...
- 使用google的GSON解析json格式的数据
GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...