Redis报错UnrecognizedPropertyException: Unrecognized field问题
目录
- 1. 现象
- 2. 排查
- 3. 解决
- 总结
1. 现象
Sprinhttp://www.devze.comg Boot访问Redis,取出对象时报错
报错信息:
Caused by: com.fasterXML.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "headPart"
at com.fasterxml.jackson.databind.exc.UnreYfaChLcMWcognizedPropertyException.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(BeanDeserialhttp://www.devze.comizer.java:151) &javascriptnbsp;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开头的方法容易误导序列化工具。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论