Jackson:反序列化实现泛型类的类

时间:2017-11-21 16:32:06

标签: java json generics serialization jackson

我遇到了杰克逊和通用类型的麻烦。我首先简要介绍一下我的模型:

package com.sample;

public abstract interface GenericInterface<T> extends Serializable {}

public abstract interface FooGenericInterface<T> extends GenericInterface<T>
{
    public abstract T fooMethod();
}

public abstract interface FooBar {}

public class Bar implements FooGenericInterface<FooBar>
{
    private static final long serialVersionUID = 1L;
    protected List<FooGenericInterface<?>> foos;

    public Bar() {}

    public Bar(List<FooGenericInterface<?>> foos) {
        this.foos = foos;
    }

    public List<FooGenericInterface<?>> getFoos() {
        if (foos == null) {
            foos = new ArrayList();
        }
        return foos;
    }

    public FooBar fooMethod() {
        for (FooGenericInterface<?> foo : foos) {
            foo.fooMethod();
        }
        return null;
    }
}

@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.CLASS)
public class FooObject implements FooGenericInterface<Object> {

    private String fooName;

    public FooObject() {}

    public String getFooName() {
        return fooName;
    }

    public void setFooName(String fooName) {
        this.fooName = fooName;
    }

    public Object fooMethod() {
        System.out.println("I'm FooObject " + fooName);
    }
}

我可以修改的唯一代码是FooObject类。现在,我有一个应用程序(不是我的)反序列化Bar对象。我尝试将这个json字符串传递给它:

{
        "foos": [
            {
                "com.sample.FooObject": {
                    "fooName": "Bob"
                }
            }
        ]
}

但是我收到了这个错误:

  

org.codehaus.jackson.map.JsonMappingException:无法解析类型   id&#39; com.sample.FooObject&#39;成为[简单类型,类   com.sample.FooGenericInterface&LT;&java.lang.Object中GT;]

现在,考虑到我只能编辑FooObject类(因为模型的其余部分在第三方反序列化应用程序中),我该如何解决这个问题?

提前谢谢。

0 个答案:

没有答案