The role of serialVersionUID of serialized interface Serializable in Java

Original URL: The role of serialVersionUID in the serialization interface Serializable in Java

Introduction

        This article introduces the role of serialVersionUID of the serialization interface Serializable in Java.

Serialization and deserialization

meaning

  • Serialization: convert java objects into byte sequences.
  • Deserialization: convert byte sequence to java object.

Serialization serves two purposes:

  1. Storage: Serialize java objects into bytecodes and save them to disk or Redis, etc., and deserialize them into java objects when needed
  2. RPC: Serialize java objects into bytecodes for transmission over the network

serialVersionUID

        Write the serialVersionUID into the file during serialization, and check whether the serialVersionUID in the file is the same as the serialVersionUID of the class during deserialization. If it is the same, it can be deserialized, otherwise an exception will be thrown and the deserialization will fail.

        If the serialVersionUID is not specified, then when the object is serialized, the JVM will automatically generate the serialVersionUID according to the package name, class name, variable, parameter, return value, etc. If we modify the members of the object, the serialVersionUID will change. When the object is deserialized, the two values ​​are not equal, and an error will be reported.

        So you need to manually specify the serialVersionUID:

private static final long serialVersionUID = 1L;

example

Example 1: Normal usage

Entity

package com.knife.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private static final long serialVersionUID = 333225L;

    private Long id;

    private String userName;

}

Controller

package com.knife.controller;

import com.knife.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@RestController
public class TestController {

    @GetMapping("serialize")
    public String serialize() {
        User user = new User();
        user.setId(2L);
        user.setUserName("Tony");

        try {
            FileOutputStream fos = new FileOutputStream("User.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(user);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }

        return "serialize success";
    }

    @GetMapping("deSerialize")
    public String deSerialize() {
        User user;
        try {
            FileInputStream fis = new FileInputStream("User.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            user = (User) ois.readObject();
            ois.close();
            System.out.println(user.toString());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return e.getMessage();
        }

        return "deSerialize success";
    }
}

test

1. Serialization

At this point, the backend generates User.txt

2. Deserialization

backend output

User(id=2, userName=Tony)

Example 2: An error is reported when the serialVersionUID is not specified

        Remove the serialVersionUID from the Entity, serialize it, then modify the Entity, add a field, and then deserialize it. An error will be reported at this time.

Remove serialVersionUID from Entity, as follows:

package com.knife.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private Long id;

    private String userName;

}

1. Serialization  

Generate User.txt at this time

2. Add fields and restart the application

Entity adds a field

package com.knife.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private Long id;

    private String userName;
    
    private String age;

}

Restart the application

3. Deserialization

It can be found that deserialization reports an error. Reason: Incompatible native classes: serialVersionUID = 3441701338525863619 for stream, serialVersionUID = 2482712775234331218 for native class

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/127774141