To put it simply, Java's serialization mechanism verifies version consistency by judging the serialversionuid of the class at runtime. During deserialization, the JVM compares the serialversionuid In the byte stream with the serialversionuid of the corresponding local entity (class). If they are the same, they can be deserialized, otherwise, the serialization version is inconsistent.
When the entity (class) implementing the java. Io. serializable interface does not explicitly define a name named serialversionuid, the type isLongJava serialization mechanism will automatically generate a serialversionuid Based on the compiled class for comparison. In this case, only the same compiled class will generate the same serialversionuid.
If we do not want to forcibly divide the software version by compiling, that is, the entity implementing the serialization interface can be compatible with the previous version. If the class is not changed, we need to explicitly define a class named serialversionuid, type:LongSerialized entities that do not modify the value of this variable can be serialized and deserialized to each other.
The above two paragraphs are taken from NLP. I was lucky to see it yesterday. Now I recall the scenario as follows:
Environment:
1) distributed: Multiple resin servers;
2) The serialized Java class does not display the definition serialversionuid;
3) redis is used as the cache (serialized objects are stored in the redis cache );
During usage, the version is incompatible during serialization, that is, the serialversionuid is abnormal. Because serialversionuid is not displayed and defined, the serialversionuid of classes in different resin classes is different in the distributed environment, deserialization is easy to report this exception.
Therefore, it is best to display the serial number definition during serialization.