gpt4 book ai didi

java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : No value type configured for ObjectReader

转载 作者:行者123 更新时间:2023-11-30 01:46:07 35 4
gpt4 key购买 nike

我在执行以下代码时收到com.fasterxml.jackson.databind.exc.InvalidDefinitionException:没有为ObjectReader配置值类型问题

jackson 版本是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]

更新:您能否解释一下为什么我会收到此异常?这是因为我没有提供 ClassTypeReference 吗?

最佳答案

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader

意味着,Jackson 不知道您想要反序列化为哪种类型。它不起作用,因为 Type Erasure in Java .

当你绑定(bind)一个类时它就会起作用:

public static <T> T parsingData(String body) throws IOException {
return new ObjectMapper().readerFor(A.class).readValue(body);
}

或者使用Class参数:

public static <T> T parsingData(String body, Class<T> clazz) throws IOException {
return new ObjectMapper().readerFor(clazz).readValue(body);
}

查看有关 readValue 方法的类似问题及其解决方法:How do I parametrize response parsing in Java?

要理解 readValue 方法,我们需要看一下 documentation :

Method that binds content read from given JSON string, using configuration of this reader. Value return is either newly constructed, or root value that was specified with withValueToUpdate(Object).

但是在您的代码中您使用了纯阅读器,没有任何配置。

如果您不知道类型,只需将 JSON 反序列化为 JsonNode 这可能是最通用的方法:

JsonNode root = new ObjectMapper().readTree(json);

关于java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : No value type configured for ObjectReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57919135/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com