gpt4 book ai didi

java - Jackson MRBean 不工作,无法构造实例

转载 作者:行者123 更新时间:2023-12-02 06:19:54 24 4
gpt4 key购买 nike

我有这个代码:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new MrBeanModule());

这个测试用例:

SearchResultContainer src = objectMapper.readValue("{}", SearchResultContainer.class);

SearchResultContainer 是一个 POJO 接口(interface)(这就是此处使用 MRBean 的全部原因)。

为什么会失败并出现以下异常?

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.ctc.rets.dto.search.SearchResultContainer` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (String)"{}"; line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at com.ctc.web.client.CustomRestTemplateTest.test1(CustomRestTemplateTest.java:25)

最佳答案

在 pom.xml 中发现 MRBean 版本不正确。

修复:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-mrbean -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-mrbean</artifactId>
<version>2.9.8</version>
</dependency>

关于java - Jackson MRBean 不工作,无法构造实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55842739/

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