javacom。fasterxml。杰克逊。数据绑定。exc.InvalidDefinitionException:没有为ObjectReader配置值类型
我在执行下面的代码时收到com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader
问题
杰克逊的版本是jackson-databind-2.9.9.jar
public <T> T parsingData(String body) {
try {
return getObjectMapper().reader().readValue(body);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
下面是确切的异常,我进入printStackTrace
,可以找到异常中公开的String body
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader
at [Source: (String)"{
"timestamp": "2019-06-04T09:36:50.086+02:00",
"path": "/api/check/85358/checking/246syb-f3f2-4756-91da-dae3e8ce774b/test/22462da-c4e2-45ca-bd27-246/rows/8bc3965a-ae22-4d7f-b770-262sgs24/port",
"status": 400,
"error": "Internal Server Error",
"message": "[owner.firstName2:size:1:30, owner.lastName2:size:1:30]",
"errorCode": 200000
}
"; line: 1, column: 1]
更新:您能解释一下我为什么会收到此异常吗?这是因为我没有提供Class
还是TypeReference
# 1 楼答案
意味着,
Jackson
不知道要反序列化到哪种类型。由于Type Erasure in Java,它无法工作当您绑定一个类时,它将起作用:
或使用
Class
参数:参见
readValue
方法的类似问题以及如何解决:How do I parametrize response parsing in Java?为了理解
readValue
方法,我们需要看看documentation:但在您的代码中,您使用的是纯阅读器,没有任何配置
如果您不知道某个类型,只需将
JSON
反序列化为JsonNode
,这可能是最通用的方式: