修改serialVersionUID 值 导致 java.io.

2020-04-03  本文已影响0人  炒面Z

异常信息

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());
    }
    
    
}
上一篇下一篇

猜你喜欢

热点阅读