我有此代码:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new MrBeanModule());
和这个测试用例:
SearchResultContainer src = objectMapper.readValue("{}", SearchResultContainer.class);
SearchResultContainer
是POJO 接口 (这就是在这里使用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)
答案 0 :(得分:0)
在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>