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异常 因为这个异常抛出俩种情况:一是“拒绝访问”,二是“系统找不到指定路径” 这里只讲明什么时候抛拒绝访问,什么时候抛找不到指定路径. 原 ...
随机推荐
- Shell计算器
#!/bin/bash # filename : jisuan.sh # description : add, subtract, multiply, and divide print_usage() ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 天兔插件监控mysql
Lepus3.8-天兔mysql数据库监控系统搭建 原创ixhao2016-08-24 02:36:23评论(11)3183人阅读 Lepus3.8-天兔mysql数据库监控系统搭建 lepus是一款 ...
- Java并发:等待事件发生后所有线程继续执行
等待某一个指定的事件发生后,才让多个等待的线程继续执行,以下是我能想到的几个方法,欢迎讨论.指正. 1.闭锁CountDownLatch 闭锁是典型的等待事件发生的同步工具类,将闭锁的初始值设置1,所 ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- List和Set排序的实现
List.Set.Map的区别 List和Set继承了Collection接口. List以特定索引来存取元素,可以有重复元素.Set不能存放重复元素(用对象的equals()方法来区分元素是否重复) ...
- PHP、jQuery、AJAX和MySQL 数据库实例
index.html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- PBR探索
原理 根据能量守恒,以及一系列光照原理得出微表面BRDF(Bidirectional Reflectance Distribution Function)公式 // D(h) F(v,h) G(l,v ...
- 我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法
面试题:为一个充满整个屏幕的LinearLayout布局指定背景图,是否可以让背景图不充满屏幕?请用代码描述实现过程. 解决此题,可以使用嵌入(Inset)图像资源来指定图像,然后像使用普通图像资源一 ...
- 记一次Net软件逆向的过程(经典)
查壳 1.先看下目录结构: 2.查下,是什么语言 ==> Net的,那不用说了,肯定能破解(毕竟是老本行嘛~) 混淆与反混淆 3.dnSpy打开后发现很多变量是乱码 4.用de4dot跑一波 5 ...