Redis报错UnrecognizedPropertyException: Unrecognized field问题
1. 现象
Spring Boot访问Redis,取出对象时报错
报错信息:
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "headPart"
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3129)
at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73)
... 39 common frames omitted
2. 排查
最开始以为忘了写implements Serializable
,检查后不是该问题。
然后以为是不是忘了写get set方法,检查后也不是。
最后发现了细节提示:
Unrecognized field "headPart"
也就是说识别不了属性headPart
恍然大悟,原来我写了一个方法getHeadPart,但是并没有headPart属性
3. 解决
将getHeadPart改为makeHeadPart即可…
像jackson这种序列化工具是依赖反射来识别方法属性的,get开头的方法容易误导序列化工具。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Redis报错:Could not create server TCP 
这篇文章主要介绍了Redis报错:Could not create server TCP listening socket 127.0.0.1:6379: bind:解决方法,是安装与启动Redis过程中比较常见的问题,需要的朋友可以参考下2023-06-06
最新评论