解决方案: 在类中显式指定

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接口的注意事项的更多相关文章

  1. java.io.NotSerializableException:异常解决

    创建的pojo类未实现Serializable接口,启动服务器会报此异常. 解决此问题 添加一个Serializable接口即可 1 import java.io.Serializable; 2 3 ...

  2. spark 解决错误java.io.InvalidClassException

    今天遇到一个现场问题,任务报错java.io.InvalidClassException.在开发环境是没有报错的,正式环境报错.大概类似于下面这样(非报错原文,摘自网上同类博客) java.io.In ...

  3. Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible

    项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...

  4. 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法

    今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...

  5. Java基础知识强化104:Serializable接口 与 Parcelable接口

    1. 什么是 序列化 和 反序列化 ?     序列化 :序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间.序 ...

  6. java.io.WriteAbortedException异常

    java.io.WriteAbortedException异常 未实现 public interface Serializable 接口的类将无法使其任何状态序列化或反序列化. 可序列化类的所有子类型 ...

  7. java.io.InvalidClassException

    java.io.InvalidClassException public class PetroleumCoke implements Serializable { private static fi ...

  8. java.io.InvalidClassException: com.master.CurrentMessages; local class incompatible:

    报错信息如下: java.io.InvalidClassException: com.master.CurrentMessages; local class incompatible: stream ...

  9. java.io.FileNotFoundException异常,一是“拒绝访问”,二是“系统找不到指定路径”

    关于java.io.FileNotFoundException异常 因为这个异常抛出俩种情况:一是“拒绝访问”,二是“系统找不到指定路径” 这里只讲明什么时候抛拒绝访问,什么时候抛找不到指定路径. 原 ...

随机推荐

  1. 常见Linux/Unix开发辅助命令什锦

    很多零碎命令集锦: 1. 怎样通过命令下载ftp文件 read -s -p "Your passwd: " Passwd; wget --user=YourUserName --p ...

  2. Trie|如何用字典树实现搜索引擎的关键词提示功能

    Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...

  3. OpenOffice/LibreOffice的行距问题

    OpenOffice和LibreOffice的默认行距(行间距)都很宽,可以通过以下方法设置. 格式 -> 页面 -> 文字网格 -> 不使用网格

  4. bzoj3531【SDOI2014】旅行

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 850  Solved: 433 [Submit][Status ...

  5. Android Studio 使用笔记:查看类结构和继承关系

    选中类 ,按下F4,可以打开类的源代码 在 Eclipse 中我们可以使用 Ctrl + O 组合热键查看类的结构,Android Studio 中也可以做到. View -> Tool Win ...

  6. LNMP环境搭建之php安装,wordpress博客搭建

    LNMP环境搭建之php安装,wordpress博客搭建 一.介绍: 1.什么是CGI CGI全称是"通用网关接口"(Common Gateway Interface),HTTP服 ...

  7. linux卸载一个源码包安装的软件的流程

    完全卸载memcached的方法(CentOS) 我的大内存vps(centos系统)曾经安装过memcached,想给论坛提速,实际上不但没有明显效果,反倒耗费内存,看着碍眼,于是想卸载,于是网上各 ...

  8. Labview新建项目步骤

    打开Labview软件,点击工具栏中文件选项卡,如图所示. 2 点击新建一个空白项目. 3 此时为未命名项目,按下Ctrl+S保存项目到自己指定的目录并完成命名. 4 如图示在我的电脑上点击右键,新建 ...

  9. sgu Theodore Roosevelt【判断点是否在凸多边形内模板】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=253 http://acm.hust.edu.cn/vjudge/contest/vi ...

  10. 九度OJ 1254:N皇后问题 (N皇后问题、递归、回溯)

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:765 解决:218 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一 ...