所以我有json文件,例如:
{ "id" : 1,
"includingJson" : {"foo" : "bar"}
}
我有一些DTO,例如:
...
public class SubscriptionDTO extends AbstractDTO{
private Long id;
private JsonNode includingJson;
但是我尝试通过代码将JSON转换为POJO
public static <T> T jsonStringToDto(Class<?> dtoClass, String jsonContent) {
ObjectMapper mapper = new ObjectMapper();
try {
return (T) mapper.readValue(jsonContent, dtoClass);
} catch (IOException e) {
log.error(e);
}
return (T) new Object();
}
我收到了错误消息Can not construct instance of org.codehaus.jackson.JsonNode, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
所以主要的问题-我该如何解决?
答案 0 :(得分:0)
似乎您导入了错误的JsonNode,该版本较旧。正确使用的类是com.fasterxml.jackson.databind.JsonNode
,它可以工作。在Jackson 2.8上进行了测试。另外,为了确保正常工作,最好是DTO类具有getter,setter和no-arg构造函数。因此,请更新版本。
答案 1 :(得分:0)
您可以将DTO编辑为如下形式:
public class SubscriptionDTO extends AbstractDTO{
private Long id;
private InnerDTO includingJson;
//..getters, setters
}
您的InnerDTO类:
public class InnerDTO extends AbstractDTO{
private String foo;
//..getters, setters
}