java - 如何在 DTO 模型中实现 JsonNode 类型的字段
问题描述
所以我有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
所以主要问题 - 我该如何解决这个问题?
解决方案
看来您导入了错误的 JsonNode,它是较旧的。正确使用的类是com.fasterxml.jackson.databind.JsonNode
,它可以工作。用杰克逊 2.8 对其进行了测试。此外,为了确保正常工作,最好 DTO 类具有 getter、setter 和无参数构造函数。所以,更新版本。
推荐阅读
- ios - 在 React-Native 应用程序上添加隐私视图
- android - 如何在单击操作按钮时播放音乐
- intellij-idea - 隐藏intellij idea 2018的标签外观
- python - Python:从函数和数组创建矩阵
- ios - 在 Swift 中在 ArcGIS 上加载地图的问题
- vue.js - 如何在 vue-i18n 翻译字符串中引用父道具值?
- react-native - j中心还是别的什么?
- jquery - 在动画暂停时使用 jquery 更改动画延迟在 Safari 上不起作用,但在其他任何地方都有效
- python - Python中最有效的算法生成所有6x6(0,1)矩阵的列和行总和小于2?
- r - R跨间接保留符号名称