Jackson - How to specify a single implementation for interface-referenced deserialization?

Max Schmidt picture Max Schmidt · Oct 2, 2012 · Viewed 30.9k times · Source

I want to deserialize a JSON-Object with Jackson. Because the target is an interface I need to specify which implementation should be used.

This information could be stored in the JSON-Object, using @JsonTypeInfo-Annotation. But I want to specify the implementation in source code because it's always the same.

Is this possible?

Answer

David Grant picture David Grant · Oct 2, 2012

Use a SimpleAbstractTypeResolver:

ObjectMapper mapper = new ObjectMapper();

SimpleModule module = new SimpleModule("CustomModel", Version.unknownVersion());

SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
resolver.addMapping(Interface.class, Implementation.class);

module.setAbstractTypes(resolver);

mapper.registerModule(module);