Redis | 使用redis儲存物件反序列化異常SerializationFailedException
阿新 • • 發佈:2019-01-27
案例
使用Redis進行物件儲存,在處理業務邏輯的時候,叢Redis獲取物件發現反序列化失敗,丟擲如下異常:
Caused by: org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.InvalidClassException: weixin.popular.bean.token.Token; local class incompatible: stream classdesc serialVersionUID = -584152862903853930, local class serialVersionUID = 8841433872811285796
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:82 )
at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:318)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:58)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207 )
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:91)
at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
at com.ppmoney.geedai.wechat.service.wechat.impl.GetAccessTokenServiceImpl.get(GetAccessTokenServiceImpl.java:37)
at com.ppmoney.geedai.wechat.service.wechat.impl.CustomMsgServiceImpl.send(CustomMsgServiceImpl.java:32)
... 82 common frames omitted
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.InvalidClassException: weixin.popular.bean.token.Token; local class incompatible: stream classdesc serialVersionUID = -584152862903853930, local class serialVersionUID = 8841433872811285796
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36)
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:80)
... 90 common frames omitted
Caused by: java.io.InvalidClassException: weixin.popular.bean.token.Token; local class incompatible: stream classdesc serialVersionUID = -584152862903853930, local class serialVersionUID = 8841433872811285796
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.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:70)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:73)
... 92 common frames omitted
解決方案
經排查,原因是存放的物件忘了新增序列化號,導致儲存的序列化編號和反序列化編號不一致,所以反序列化異常。
只需要把序列化編號新增上即可,記得清理Redis快取:
private static final long serialVersionUID = 8841433872811285796L;