UnrecognizedPropertyException: Unrecognized field “@type“ not marked as ignorable

心已赠人 2022-09-09 13:46 343阅读 0赞
  • 问题背景:Redis里的数据取出来反序列成对象,报错 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "@type" (class com.xx.common.xx.xxx.xxx.LoginUser)
    在这里插入图片描述
  • 有可能的原因
  • 1、@type类型与本地对象类型不同,比如:字符串 @type是com.xx.common.xx.xxx.xxx.LoginUser,但你本地对象是com.yy.common.xx.xxx.xxx.LoginUser
  • 2、数据字段和本地对象对应相差太大。
  • 解决方案,代码如下所示

    String str = “{\”os\”:\”Windows 10 or Windows Server 2016\”,\”@type\”:\”com.xx.common.xx.xx.xx.LoginUser\”,……….}”;

    1. ObjectMapper objectMapper = new ObjectMapper();
    2. //这句代码
    3. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    4. LoginUser loginUser = objectMapper.readValue(str, LoginUser.class);
    5. System.out.println(loginUser);

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读