Serializable接口 和 serialVersionUID 的关系

  1. 结论建议实现了Serializable 手动生成 serialVersionUID

  2. 一般认为实现了Serializable 接口,不手动生成serialVersionUID不会出问题。

  3. 如果没有手动生成,序列化和反序列化的时候,通过类所有字段算一个serialVersionUID出来,但是据说不同jvm可能算的不一样。

  4. 实现了Serializable,但是不手动生成serialVersionUID, eclipse 默认会警告 ,idea默认不会警告

  5. 对象反序列化的时候

    • serialVersionUID相等,字段不相等,不能反序列化
    • serialVersionUID不相等,字段相等,不能反序列化
    • serialVersionUID不相等,字段不相等,不能反序列化
    • 只有serialVersionUID相等,并且字段完全相等才能序列化
  6. 如果帮你需要不改动字段,并且让前后版本java序列化不兼容,可以修改serialVersionUID

  7. 如果你修改了字段,但是你忘了修改serialVersionUID,也是无所谓的事。

  8. 测试代码

    
        /**
         * 测试序列化
         * @throws IOException
         */
        @Test
        public void writeObject() throws IOException {
            File file = new File("C:\\Users\\ZHANGYUKUN\\Desktop\\java.data");
            FileOutputStream fileInputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileInputStream);
    
    
            Goods goods = Goods.randomGoods();
            objectOutputStream.writeObject( goods );
            System.out.println( JSONUtil.toJsonStr( goods ) );
        }
        /**
         * 读取序列化
         * @throws IOException
         * @throws ClassNotFoundException
         */
        @Test
        public void read() throws IOException, ClassNotFoundException {
            File file = new File("C:\\Users\\ZHANGYUKUN\\Desktop\\java.data");
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
    
            Object goods = objectInputStream.readObject();
            System.out.println( JSONUtil.toJsonStr( goods ) );
    
        }
    
    
  9. goods对象

    package com.lomi.entity;
    
    import com.lomi.utils.CodeUtil;
    
    import cn.hutool.core.util.IdUtil;
    import com.lomi.utils.IdUtils;
    
    import java.io.Serializable;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Goods implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
    
        private Long id;
    
        private Date createDate;
    
        private String name;
    
        private Integer stock;
    
    	private String des;
    
        private String des2;
    
        private String data;
    
        public Date getCreateDate() {
            return createDate;
        }
    
        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }
    
        public Goods(String name ) {
        	this.name = name;
        }
        public Goods() {
    
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public String name(int  i) {
            return "name????" + i;
        }
    
        public Goods setName(String name) {
            this.name = name == null ? null : name.trim();
            return this;
        }
    
        public Integer getStock() {
            return stock;
        }
    
        public void setStock(Integer stock) {
            this.stock = stock;
        }
    
        public String getDes() {
            return des;
        }
    
        public void setDes(String des) {
            this.des = des == null ? null : des.trim();
        }
    
        public String getData() {
            return data;
        }
    
        public void setData(String data) {
            this.data = data == null ? null : data.trim();
        }
    	public String getDes2() {
    		return des2;
    	}
    
    	public void setDes2(String des2) {
    		this.des2 = des2;
    	}
    
    
        public static Goods randomGoods() {
        	Goods goods = new Goods();
    		goods.setId(IdUtils.nextId());
    		goods.setData(  CodeUtil.getRandomNum18() );
    		goods.setDes( CodeUtil.randomCode(16) );
    		goods.setDes2( CodeUtil.randomCode(16) );
    		goods.setName( CodeUtil.randomCode(3) );
    		goods.setStock( Integer.valueOf( CodeUtil.randomNumCode(4) ) );
            goods.setCreateDate( new Date());
    
        	return goods;
        }
    
    
    
    
    }
    

posted on 2023-04-03 21:50  zhangyukun  阅读(60)  评论(0编辑  收藏  举报

导航