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

前端 未结 3 765
感动是毒
感动是毒 2020-12-05 09:40

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

3条回答
  •  一向
    一向 (楼主)
    2020-12-05 10:04

    There is another approach that will work if you have just single interface implementation.

    public class ClassYouWantToDeserialize {
        @JsonDeserialize(as = ImplementationClass.class)
        private InterfaceClass property;
    ...
    }
    

    This question was answered a while ago but I want to give you another option that doesn't require to tune ObjectMapper and also much simpler then @JsonTypeInfo annotation.

提交回复
热议问题