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

时间:2015-12-16 16:13:51

标签: android json class jackson msgpack

我正在从服务器向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"]

如果我更改变量&#34;数据&#34;说MAP<String, String> data然后它工作正常,但是,数据属于未知类型! (通常HashMap或数组可能是String,而不是其他类)。

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

如何指定未知类型?

2 个答案:

答案 0 :(得分:2)

所以我将课程改为;

public class myClass{

    public String status;
    public Object data;

}

我现在只测试对象类型。我不知道为什么我之前没试过这个!

答案 1 :(得分:1)

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

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