Java中serialVersionUID

时间:2021-07-15 15:10:03

报错信息如下:

Adds a default serial version ID to the selected type.

Use this option to add a user-defined ID in combination with
 custom serialization code if the type did undergo structural
 changes since its first release.

推荐两篇文章:http://zengxiankang2011.blog.163.com/blog/static/1783603192011594938588/

http://www.mkyong.com/java-best-practices/understand-the-serialversionuid/

Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException)

通过再类中添加 private static final long serialVersionUID=1L;即可