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

  • 问题背景: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\",..........}";
        ObjectMapper objectMapper = new ObjectMapper();
         //这句代码
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        LoginUser loginUser = objectMapper.readValue(str, LoginUser.class);
        System.out.println(loginUser);

posted on 2021-09-01 15:52  愤怒的苹果ext  阅读(768)  评论(0编辑  收藏  举报

导航