-
Here is my code: import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJackson {
static interface Entity {
String getName();
}
@Test
public void test() throws JsonProcessingException {
String json = "{\"name\": \"good\"}";
ObjectMapper objectMapper = new ObjectMapper();
Entity entity = objectMapper.readValue(json, Entity.class);
// Entity entity = objectMapper.readValue(json, new TypeReference<Entity>(){});
System.out.println("name: " + entity.getName());
}
} Get exception: Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `io.gitlab.donespeak.turtal.TestJackson$Entity` (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)"{"name": "good"}"; line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
at io.gitlab.donespeak.turtal.TestJackson.test(TestJackson.java:23)
at io.gitlab.donespeak.turtal.TestJackson.main(TestJackson.java:10)
Process finished with exit code 1 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Discussions is not a feature to get coding advice. Please read https://docs.github.com/en/discussions Stackoverflow.com is probably the best way to discuss issues about why your code is not working as you expect. The issue you are hitting here is likely down to the fact that your interface does not expose a way to set the name. Please understand that deserialization requires Jackson to use Java methods to create object instances and set values on those instances. Your interface has neither. You should read https://www.baeldung.com/jackson-annotations#jackson-polymorphic-type-handling-annotations |
Beta Was this translation helpful? Give feedback.
-
+1 for above. Interfaces are abstract so one cannot create instances without some more information. In addition to SO:
|
Beta Was this translation helpful? Give feedback.
Discussions is not a feature to get coding advice. Please read https://docs.github.com/en/discussions
Stackoverflow.com is probably the best way to discuss issues about why your code is not working as you expect. The issue you are hitting here is likely down to the fact that your interface does not expose a way to set the name. Please understand that deserialization requires Jackson to use Java methods to create object instances and set values on those instances. Your interface has neither.
You should read https://www.baeldung.com/jackson-annotations#jackson-polymorphic-type-handling-annotations