stream classdesc serialVersionUID = 1, local class serialVersionUID = 2

原创转载请注明出处:http://agilestyle.iteye.com/blog/2301301

 

先看一个简单的例子

package org.fool.test;

import java.io.Serializable;

public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private long id;
	private String username;
	private String password;

	public User() {
	}

	public User(long id, String username, String password) {
		this.id = id;
		this.username = username;
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

 这里先创建一个User对象,序列化UID为1L

package org.fool.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableTest {
	public static void main(String[] args) throws Exception {
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("serial.txt"))));
		oos.writeObject(new User(1L, "hello", "world"));
		oos.close();
		
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("serial.txt"))));
		User user = (User) ois.readObject();
		System.out.println(user);
		ois.close();
	}
}

 接着使用IO进行读写,查看输出

User [id=1, username=hello, password=world]

stream classdesc serialVersionUID = 1, local class serialVersionUID = 2_第1张图片
本地工程下面生成了serial.txt文件,然后将User对象的序列化UID改为2L

public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;

	private long id;
	private String username;
	private String password;
......

 同时注释掉原先的IO写操作,只进行读操作

public class SerializableTest {
	public static void main(String[] args) throws Exception {
		//ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("serial.txt"))));
		//oos.writeObject(new User(1L, "hello", "world"));
		//oos.close();
		
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("serial.txt"))));
		User user = (User) ois.readObject();
		System.out.println(user);
		ois.close();
	}
}

 查看输出

Exception in thread "main" java.io.InvalidClassException: org.fool.test.User; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
	at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
	at org.fool.test.SerializableTest.main(SerializableTest.java:18)

Java通过一个名为UID(stream unique identifier)来控制,这个UID是隐式的,它通过类名,方法名等诸多因素经过计算而得,理论上是一一映射的关系,也就是唯一的。如果UID不一样的话,就无法实现反序列化了,并且将会得到InvalidClassException。这就是通常项目中版本升级会遇到的问题,很有可能缓存或者数据库的数据是以1L的版本写入,之后由于项目版本升级,UID变为了2L,在2L的基础上进行读操作,就会抛出这个异常,也就是序列化兼容性问题,解决的办法就是将UID版本统一,并且将原先缓存或数据库的数据清空,同时以当前最新的UID版本重新写入。

 

 

你可能感兴趣的:(Java)