我想用Jackson反序列化一个JSON对象.因为目标是一个接口,所以我需要指定应该使用哪个实现.
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?
我想用Jackson反序列化一个JSON对象.因为目标是一个接口,所以我需要指定应该使用哪个实现.
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?
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);