使用MSGPack Core和Jackson Mapper的Android-解码未知类型的类变量



我正在从服务器向Android发送/接收一个自定义类,该类为;

import org.msgpack.value.Value;
public class myClass {
    public String status;
    public Value data;
}

问题是我总是犯错误;

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.msgpack.value.Value, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
        at [Source: java.io.BufferedInputStream@f478759; line: -1, column: 100] (through reference chain:xxx.xxxxxxxxxx.xxx.xxxxxx.myClass["data"]

如果我将变量"data"改为MAP<String, String> data,那么它可以正常工作,但是,数据是未知类型的!(通常情况下,HashMap或Array可能是String,而不是其他类)。

MessagePackFactory factory = new MessagePackFactory();
ObjectMapper mapper = new ObjectMapper(factory);
myClass response = mapper.readValue(inputStream, myClass.class);

如何指定未知类型?

所以我把类改为;

public class myClass{
    public String status;
    public Object data;
}

现在我只测试Object类型。我不知道为什么我以前没有尝试过!

org.msgpack.value.value是一个接口。

使用ObjectMapper对值进行反序列化时,目标必须是具有默认构造函数的类。其他OM无法创建目标对象。

最新更新