BAEL-871 What is serialVersionUID? (#2203)

* Bean Injection in Spring – ashrafmsokkar@gmail.com

* Bean Injection in Spring – ashrafmsokkar@gmail.com

* BAEL-871: What is serialVersionUID?

* BAEL-871: What is serialVersionUID?

* BAEL-871 What is serialVersionUID?

* BAEL-871 Implemented feedback
This commit is contained in:
fromalexwithluv
2017-07-06 23:45:03 -07:00
committed by Grzegorz Piwowarek
parent f3c99a7fcf
commit 5a184d4e0d
5 changed files with 150 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
package com.baeldung.deserialization;
import java.io.Serializable;
public class AppleProduct implements Serializable {
private static final long serialVersionUID = 1234567L; // user-defined (i.e. not default or generated)
// private static final long serialVersionUID = 7654321L; // user-defined (i.e. not default or generated)
public String headphonePort;
public String thunderboltPort;
public String lighteningPort;
public String getHeadphonePort() {
return headphonePort;
}
public String getThunderboltPort() {
return thunderboltPort;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.deserialization;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Base64;
public class DeserializationUtility {
public static void main(String[] args) throws ClassNotFoundException, IOException {
String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADmxpZ2h0ZW5pbmdQb3J0cQB+AAFMAA90aHVuZGVyYm9sdFBvcnRxAH4AAXhwdAARaGVhZHBob25lUG9ydDIwMjBwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA==";
System.out.println("Deserializing AppleProduct...");
AppleProduct deserializedObj = (AppleProduct) deSerializeObjectFromString(serializedObj);
System.out.println("Headphone port of AppleProduct:" + deserializedObj.getHeadphonePort());
System.out.println("Thunderbolt port of AppleProduct:" + deserializedObj.getThunderboltPort());
}
public static Object deSerializeObjectFromString(String s) throws IOException, ClassNotFoundException {
byte[] data = Base64.getDecoder().decode(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.deserialization;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Base64;
public class SerializationUtility {
public static void main(String[] args) throws ClassNotFoundException, IOException {
AppleProduct macBook = new AppleProduct();
macBook.headphonePort = "headphonePort2020";
macBook.thunderboltPort = "thunderboltPort2020";
String serializedObj = serializeObjectToString(macBook);
System.out.println("Serialized AppleProduct object to string:");
System.out.println(serializedObj);
}
public static String serializeObjectToString(Serializable o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
}