Jackson MRBean 不工作,无法构造实例
Jackson MRBean not working, Cannot construct instance
我有这个代码:
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>
我有这个代码:
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>