java.io.Serializable 序列化问题【原】
java.io.Serializable 序列化问题
Person.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package a.b.c; public class Person implements java.io.Serializable{ // private static final long serialVersionUID = 2481632967198330813L; String field1; String field2; String field3; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; } public String getField3() { return field3; } public void setField3(String field3) { this.field3 = field3; } }
Student.java Student继承自Person,但为了清晰表示,按Person一模一样又覆盖了一遍
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package a.b.c; public class Student extends Person implements java.io.Serializable { // private static final long serialVersionUID = 2481632967198330813L; String field1; String field2; String field3; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public String getField2() { return field2; } public void setField2(String field2) { this.field2 = field2; } public String getField3() { return field3; } public void setField3(String field3) { this.field3 = field3; } }
SerializableWriter.java
package a.b.c; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializableWriter { public static void main(String[] args) throws Exception{ write(); } public static void write() throws Exception{ Student stu= new Student(); stu.setField1("aaa111"); stu.setField2("bbb222"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("C:/Users/King/Desktop/jx.txt")); objectOutputStream.writeObject(stu); objectOutputStream.flush(); objectOutputStream.close(); } }
SerializableReader.java
package a.b.c; import java.io.FileInputStream; import java.io.ObjectInputStream; public class SerializableReader { public static void main(String[] args) throws Exception{ read(); } public static void read() throws Exception{ ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("C:/Users/King/Desktop/jx.txt")); Object obj=objectInputStream.readObject(); Person person=(Person)obj; System.out.println(person.getField1()); System.out.println(person.getField2()); objectInputStream.close(); } }
打印结果
aaa111
bbb222
总结
- 只要版本号一致,且Student就算删掉field3的属性和方法,仍旧可以把序列化后的Student反序列化成Student.
- 只要版本号一致,且Student 继承自父类 Person,那么就可以把序列化后的Student反序列化成Person.
自己项目经历:
esb_new.jar包中的head多加了一个password属性(这个场景是说添加属性的那个包就是新包)
然后生产上 核心 通过http连接 ESB(esb_old.jar) 去调用 电销(esb_new.jar)回调接口 ,报序列号版本不一致问题, 因为电销提供的回调接口是Spring Invoke远程方法调用,需要依赖请求对象RequestBusinessObjVo,在转对象时会用到序列化头部信息,此时旧包(esb_old.jar)丢了新包(esb_new.jar)需要的属性,所以旧包不能调新包.
但是电销(esb_new.jar)通过http连接 ESB(esb_old.jar)去调代理平台(esb_old.jar) 没报错. 是因为 电销 和ESB是http通信的,而ESB和代理平台此时通过Spring Invoke远程方法调用,仍旧用的是esb_old.jar包所以没有报序列号问号.
感觉空虚寂寞,只是因为你无所关注,无处付出。