Fork me on GitHub

Java中的序列化、反序列化-serializable和serialversionUID

在Java项目中,我们经常对实体类进行implement serializable,在实体类中又加上一句

private static final long serialVersionUID = 5177165015785112433L;

最开始只知道是跟实体类的序列化有关,但是没有深究,昨天突然想起来了,于是想知道到底是什么东西,在这里小手一抖,记录一下......


这里补充一下小知识:

  1、序列化、反序列化:

  序列化:将对象转化为字节序列的过程;

  反序列化:将字节序列反转成对象的过程。  具体看到下面的例子就明白了

  2、在io流中,有ObjectInputStream、ObjectoutputStream,这俩个在用在实体对象时,要求对象必须实现serializable,表示该对象可被序列化。

 一、首先我们尝试一下,实体类不进行implement Serializable,分别对实体类进行序列化和反序列化

  咱的实体类:

 1 package com.example.io.serializable;
 2 
 3 import lombok.Data;
 4 
 5 import java.io.Serializable;
 6 
 7 /**
 8  * @program: io
 9  * @description:
10  * @author: liuqj
11  * @create: 2020-06-17 09:41
12  **/
13 @Data
14 public class Book {
15 
16     private int id;
17     private String name;
18     private Double price;
19 }

 

 测试: 

 1     package com.example.io.serializable;
 2 
 3     import java.io.*;
 4 
 5     /**
 6      * @program: io
 7      * @description:
 8      * @author: liuqj
 9      * @create: 2020-06-17 09:43
10      **/
11     public class TestSerializable {
12         public static void main(String[] args) throws IOException, ClassNotFoundException {
13             serializable();
14             deserializable();
15     }
16     static void serializable () throws IOException {
17         Book book = new Book();
18         book.setId(1);
19         book.setName("从大爆炸到黑洞");
20         book.setPrice(100.00);
21         ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("D:\\Personal\\JavaProject\\io\\serializable.txt")));
22         outputStream.writeObject(book);
       outputStream.clouse();
23 System.out.println("序列化成功"); 24 } 25 static void deserializable() throws IOException, ClassNotFoundException { 26 ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("D:\\Personal\\JavaProject\\io\\serializable.txt"))); 27 Book book = (Book) inputStream.readObject();
       inputStream.clouse();
28 System.out.println(book); 29 }

 

运行结果:

 

 

 

 可以看到如果不对实体类进行implement Serializable,那么进行序列化的时候就会报错。

 

二、对实体进行implement Serializable,但不加serialversionUID。

1 @Data
2 public class Book implements Serializable{
3 
4     private int id;
5     private String name;
6     private Double price;
7 }

 

1、直接进行序列化、反序列化

  运行结果:

   1 序列化成功 2 Book(id=1, name=从大爆炸到黑洞, price=100.0) 3 4 Process finished with exit code 0 

2、先进行序列化,然后修改(删除)对象中的属性,再进行反序列化

 1 D:\Work\Jdk\bin\java.exe "-javaagent:D:\Work\Idea\IntelliJ IDEA 2019.3.4\lib\idea_rt.jar=54850:D:\Work\Idea\IntelliJ IDEA 2019.3.4\bin" -Dfile.encoding=UTF-8 -classpath D:\Work\Jdk\jre\lib\charsets.jar;D:\Work\Jdk\jre\lib\deploy.jar;D:\Work\Jdk\jre\lib\ext\access-bridge-64.jar;D:\Work\Jdk\jre\lib\ext\cldrdata.jar;D:\Work\Jdk\jre\lib\ext\dnsns.jar;D:\Work\Jdk\jre\lib\ext\jaccess.jar;D:\Work\Jdk\jre\lib\ext\jfxrt.jar;D:\Work\Jdk\jre\lib\ext\localedata.jar;D:\Work\Jdk\jre\lib\ext\nashorn.jar;D:\Work\Jdk\jre\lib\ext\sunec.jar;D:\Work\Jdk\jre\lib\ext\sunjce_provider.jar;D:\Work\Jdk\jre\lib\ext\sunmscapi.jar;D:\Work\Jdk\jre\lib\ext\sunpkcs11.jar;D:\Work\Jdk\jre\lib\ext\zipfs.jar;D:\Work\Jdk\jre\lib\javaws.jar;D:\Work\Jdk\jre\lib\jce.jar;D:\Work\Jdk\jre\lib\jfr.jar;D:\Work\Jdk\jre\lib\jfxswt.jar;D:\Work\Jdk\jre\lib\jsse.jar;D:\Work\Jdk\jre\lib\management-agent.jar;D:\Work\Jdk\jre\lib\plugin.jar;D:\Work\Jdk\jre\lib\resources.jar;D:\Work\Jdk\jre\lib\rt.jar;D:\Personal\JavaProject\io\target\classes;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.3.1.RELEASE\spring-boot-starter-data-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Work\Maven\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-jdbc\2.0.1.RELEASE\spring-data-jdbc-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-relational\2.0.1.RELEASE\spring-data-relational-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-commons\2.3.1.RELEASE\spring-data-commons-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;D:\Work\Maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Work\Maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Work\Maven\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Work\Maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Work\Maven\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\Work\Maven\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar com.example.io.serializable.TestSerializable
 2 Exception in thread "main" java.io.InvalidClassException: com.example.io.serializable.Book; 
    local class incompatible: stream classdesc serialVersionUID = -1858406188231479520, local class serialVersionUID = 4049423394988101590 3 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616) 4 at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1843) 5 at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713) 6 at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2000) 7 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535) 8 at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422) 9 at com.example.io.serializable.TestSerializable.deserializable(TestSerializable.java:27) 10 at com.example.io.serializable.TestSerializable.main(TestSerializable.java:14) 11 12 Process finished with exit code 1

 

可以看到反序列化失败,报错中显示,流中的UID和本地的UID不一致导致的反序列化失败,但我们本地并没有加serialversionUID啊?

是酱紫滴,虽然我们没加,但是实现了implement Serializable,该接口会帮我们在本地生成一个UID,但这种默认生成的方式,在进行反序列化时不顶用了,

会出现InvalidClassException异常,原因我也木知。(猜测大概是:序列化时(本地->流),会生成一次UID给到序列化后的内容中,反序列化时(流->本地)又再次生成了一次UID给本地,

此时,流中的UID和新生成的本地UID不同,所以导致无法反序列化)

 

三、实体类implement Serializable 且加上serialversionUID

1 public class Book implements Serializable{
2 
3     private static final Long serialVersionUID = -1858406188231479520L;  //注意final不能少,不然本地的UID在反序列化的过程中会被更改,这句代码就相当于没加
4     private int id;
5     private String name;
6     private Double price;
7 }

 

1、先直接进行序列化、反序列化

  结果显示:nice

1 D:\Work\Jdk\bin\java.exe "-javaagent:D:\Work\Idea\IntelliJ IDEA 2019.3.4\lib\idea_rt.jar=55570:D:\Work\Idea\IntelliJ IDEA 2019.3.4\bin" -Dfile.encoding=UTF-8 -classpath D:\Work\Jdk\jre\lib\charsets.jar;D:\Work\Jdk\jre\lib\deploy.jar;D:\Work\Jdk\jre\lib\ext\access-bridge-64.jar;D:\Work\Jdk\jre\lib\ext\cldrdata.jar;D:\Work\Jdk\jre\lib\ext\dnsns.jar;D:\Work\Jdk\jre\lib\ext\jaccess.jar;D:\Work\Jdk\jre\lib\ext\jfxrt.jar;D:\Work\Jdk\jre\lib\ext\localedata.jar;D:\Work\Jdk\jre\lib\ext\nashorn.jar;D:\Work\Jdk\jre\lib\ext\sunec.jar;D:\Work\Jdk\jre\lib\ext\sunjce_provider.jar;D:\Work\Jdk\jre\lib\ext\sunmscapi.jar;D:\Work\Jdk\jre\lib\ext\sunpkcs11.jar;D:\Work\Jdk\jre\lib\ext\zipfs.jar;D:\Work\Jdk\jre\lib\javaws.jar;D:\Work\Jdk\jre\lib\jce.jar;D:\Work\Jdk\jre\lib\jfr.jar;D:\Work\Jdk\jre\lib\jfxswt.jar;D:\Work\Jdk\jre\lib\jsse.jar;D:\Work\Jdk\jre\lib\management-agent.jar;D:\Work\Jdk\jre\lib\plugin.jar;D:\Work\Jdk\jre\lib\resources.jar;D:\Work\Jdk\jre\lib\rt.jar;D:\Personal\JavaProject\io\target\classes;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.3.1.RELEASE\spring-boot-starter-data-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Work\Maven\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-jdbc\2.0.1.RELEASE\spring-data-jdbc-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-relational\2.0.1.RELEASE\spring-data-relational-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-commons\2.3.1.RELEASE\spring-data-commons-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;D:\Work\Maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Work\Maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Work\Maven\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Work\Maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Work\Maven\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\Work\Maven\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar com.example.io.serializable.TestSerializable
2 序列化成功
3 Book(id=1, name=从大爆炸到黑洞, price=100.0)
4 
5 Process finished with exit code 0

 

2、先序列化,然后删除(修改)对象中某一个属性,再进行反序列化

  删除一个属性后的实体类

1 @Data
2 public class Book implements Serializable{
3 
4     private static final long serialVersionUID = -1858406188231479520L;
5 
6     private int id;
7     private String name;
8     //private Double price;
9 }

 

  反序列化

1 D:\Work\Jdk\bin\java.exe "-javaagent:D:\Work\Idea\IntelliJ IDEA 2019.3.4\lib\idea_rt.jar=56290:D:\Work\Idea\IntelliJ IDEA 2019.3.4\bin" -Dfile.encoding=UTF-8 -classpath D:\Work\Jdk\jre\lib\charsets.jar;D:\Work\Jdk\jre\lib\deploy.jar;D:\Work\Jdk\jre\lib\ext\access-bridge-64.jar;D:\Work\Jdk\jre\lib\ext\cldrdata.jar;D:\Work\Jdk\jre\lib\ext\dnsns.jar;D:\Work\Jdk\jre\lib\ext\jaccess.jar;D:\Work\Jdk\jre\lib\ext\jfxrt.jar;D:\Work\Jdk\jre\lib\ext\localedata.jar;D:\Work\Jdk\jre\lib\ext\nashorn.jar;D:\Work\Jdk\jre\lib\ext\sunec.jar;D:\Work\Jdk\jre\lib\ext\sunjce_provider.jar;D:\Work\Jdk\jre\lib\ext\sunmscapi.jar;D:\Work\Jdk\jre\lib\ext\sunpkcs11.jar;D:\Work\Jdk\jre\lib\ext\zipfs.jar;D:\Work\Jdk\jre\lib\javaws.jar;D:\Work\Jdk\jre\lib\jce.jar;D:\Work\Jdk\jre\lib\jfr.jar;D:\Work\Jdk\jre\lib\jfxswt.jar;D:\Work\Jdk\jre\lib\jsse.jar;D:\Work\Jdk\jre\lib\management-agent.jar;D:\Work\Jdk\jre\lib\plugin.jar;D:\Work\Jdk\jre\lib\resources.jar;D:\Work\Jdk\jre\lib\rt.jar;D:\Personal\JavaProject\io\target\classes;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.3.1.RELEASE\spring-boot-starter-data-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-jdbc\2.3.1.RELEASE\spring-boot-starter-jdbc-2.3.1.RELEASE.jar;D:\Work\Maven\repository\com\zaxxer\HikariCP\3.4.5\HikariCP-3.4.5.jar;D:\Work\Maven\repository\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-jdbc\2.0.1.RELEASE\spring-data-jdbc-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-relational\2.0.1.RELEASE\spring-data-relational-2.0.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\data\spring-data-commons\2.3.1.RELEASE\spring-data-commons-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter\2.3.1.RELEASE\spring-boot-starter-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot\2.3.1.RELEASE\spring-boot-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.1.RELEASE\spring-boot-autoconfigure-2.3.1.RELEASE.jar;D:\Work\Maven\repository\org\springframework\boot\spring-boot-starter-logging\2.3.1.RELEASE\spring-boot-starter-logging-2.3.1.RELEASE.jar;D:\Work\Maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Work\Maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\Work\Maven\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\Work\Maven\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\Work\Maven\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Work\Maven\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\Work\Maven\repository\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;D:\Work\Maven\repository\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar com.example.io.serializable.TestSerializable
2 Book(id=1, name=从大爆炸到黑洞)
3 
4 Process finished with exit code 0

 

 

 总结:

(1)对实体进行implement Serializable,表示该对象可以进行序列化;

(2)只进行implement Serializable,每次进行序列化时会自动生成一个serialVersionUID保证序列化的进行,

  在实体对象没有改变过的前提下,可以进行反序列化,如果对象属性改变过,反序列化时再次生成的UID和序列化时生成的UID不同无法进行反序列化;

(3)在进行implement Serializable后,加上private static final long serialVersionUID = xxxL; 可保证序列化之后,即使对实体对象做出了改变仍可以进行反序列化。

 

posted @ 2020-06-17 11:06  秋刀  阅读(1351)  评论(0编辑  收藏  举报