当新字段添加到POJO时,Jackson反序列化

时间:2013-11-28 20:47:43

标签: java json jackson

我有一个以JSON格式存储在缓存中的对象,它使用Jackson ObjectMapper进行序列化。现在我用新字段修改了该对象,为它们提供了setter和getter。当我尝试从缓存中读取“旧”对象并将其反序列化为Java对象时,我收到Jackson异常:

反序列化com ...类型的对象时出错。 找不到类型[simple type,class com .... Person]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)

如何避免这种行为,以便对新字段使用默认对象值?

在我通过添加新字段phone对其进行修改后,这就是我的pojo的样子。

class Person
    {
        private String firstName = "";
        private String lastName = "";
        private int age;
        private String address = "";
        private Integer salary;
        private String phone = "";

        // default constructor required for json/smile serialization
        public Person()
        {
        }

        public Person(String firstName, String lastName, int age, String address, Integer salary, String phone)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.address = address;
            this.salary = salary;
            this.phone = phone;
        }

        public String getFirstName()
        {
            return firstName;
        }

        public String getLastName()
        {
            return lastName;
        }

        public int getAge()
        {
            return age;
        }

        public String getAddress()
        {
            return address;
        }

        public Integer getSalary()
        {
            return salary;
        }

        public String getPhone()
        {
            return phone;
        }

        public void setPhone(String phone)
        {
            this.phone = phone;
        }

    }  

0 个答案:

没有答案