美文网首页
修改serialVersionUID 值 导致 java.io.

修改serialVersionUID 值 导致 java.io.

作者: 炒面Z | 来源:发表于2020-04-03 15:57 被阅读0次

    异常信息

    java.io.InvalidClassException: edu.sairobo.web.Car; 
    local class incompatible: stream classdesc serialVersionUID = -746538807804829432,
    local class serialVersionUID = -746537804829432
    

    场景

    把Car缓存至redis中的后,修改了serialVersionUID 值,反向序列化报错
    

    源码

    
    @Slf4j
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisTest {
        
        @Autowired
        private Jedis jedis;
        
        @Test
        public void object2Bytes() throws IOException, ClassNotFoundException {
            byte[] bytes = null;
            Car car = new Car().setBrandName("拖拉机").setPrice(5980000D);
            
            // object 2 byte[]
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream ops = new ObjectOutputStream(bos);
            ops.writeObject(car);
            bytes = bos.toByteArray();
            ops.close();
            bos.close();
            
            jedis.set("car1".toString().getBytes(), bytes);
        }
        
        @Test
        public void bytes2Object() throws IOException, ClassNotFoundException {
            
            byte[] bytes = jedis.get("car1".toString().getBytes());
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            Car car2 = (Car) ois.readObject();
            
            log.info(car2.toString());
        }
        
        
    }
    

    相关文章

      网友评论

          本文标题:修改serialVersionUID 值 导致 java.io.

          本文链接:https://www.haomeiwen.com/subject/mcohphtx.html