简体   繁体   English

jackson IOException:无法将类com.mycompany.models.Person $ Address(类型为非静态成员类)反序列化为Bean

[英]jackson IOException: Can not deserialize Class com.mycompany.models.Person$Address (of type non-static member class) as a Bean

I have a class that looks like this: 我有一个看起来像这样的课程:

public class Person {
    public class Address {
        private String line1;
        private String line2;
        private String zipCode;
        private String state;

        // standard public getters and setters for the class here
   }

private String name;
private String address;

// standard public getters and setters for the class here

}

and here's how I am using jackson with it. 这就是我如何使用杰克逊。

public class JsonTest {
    public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass)
throws JsonMappingException, JsonParseException, IOException {
        return m.readValue(jsonAsString, pojoClass);
    }

    public static String toJson(Object pojo, boolean prettyPrint)
throws JsonMappingException, JsonGenerationException, IOException {
        StringWriter sw = new StringWriter();
        JsonGenerator jg = jf.createJsonGenerator(sw);
        if (prettyPrint) {
            jg.useDefaultPrettyPrinter();
        }
        m.writeValue(jg, pojo);
        return sw.toString();
    }

    public static void main(String[] args) {
         Person p = new Person();
         String json = this.toJson(p, true); // converts ‘p’ to JSON just fine
         Person personFromJson = this.fromJson(json, Person.class); // throws exception!!!
    }
}

the 3rd line of the main method (where I try to convert json to Person object), throws this exception: main方法的第3行(我尝试将json转换为Person对象),抛出此异常:

IOException: Can not deserialize Class com.mycompany.models.Person$Address (of type non-static member class) as a Bean

what am I doing wrong? 我究竟做错了什么?

Because inner classes do not have a default zero argument constructor (they have a hidden reference to the outer/parent class) Jackson cannot instantiate them. 因为内部类没有默认的零参数构造函数(它们具有对外部/父类的隐藏引用),Jackson无法实例化它们。

The solution is to use static inner classes: 解决方案是使用static内部类:

public class Outer {
    static class Inner {
        private String foo;
        public String getFoo() { return foo; }
    }
}

Original Answer : 原答案

There are some issues in implementation and it seems like you can't serialize such classes, see cowtowncoder for details. 实现中存在一些问题,似乎您无法序列化这些类,请参阅cowtowncoder了解详细信息。

Correct, this used to be something you can not do with current Jackson versions (1.8 and earlier). 正确,这曾经是目前杰克逊版本(1.8及更早版本)无法做到的。 But is it absolutely necessary for inner class to be non-static? 但内部阶级绝对有必要是非静态的吗? If not, just add 'static' in declaration of Address and it'll work fine; 如果没有,只需在地址声明中添加“静态”,它就可以正常工作; problem is with the "hidden" this-pointer that non-static inner classes take via constructor. 问题在于“隐藏”这个指针,非静态内部类通过构造函数获取。

Jackson 1.9 will actually supports deserialization of simple uses of non-static inner classes, see this Jira entry . Jackson 1.9实际上支持非静态内部类的简单使用的反序列化,请参阅此Jira条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM