Saying I have an interface A, I want to use custom deserializer for all classes implement interface A, So I use code below but it doesn't work, While CustomAserializer works. So what should I do to deserialize all classes implement A using my custom deserializer. Thanks.
module.addDeserializer(A.class, new CustomADeserializer());
module.addSerializer(A.class, new CustomASerializer())
It seems you forgot to annotate your implementation classes with
@JsonDeserialize(using = ImplementationClazz.class)
to indicate that the class should be used to deserialize the abstract class or interface.The following is a simple example to deserialize an interface having multiple implementations using Jackson.
Here is my interface:
One implementation of the interface:
Second implementation:
And here is the deserializer:
You may get a StackOverflowError if you don't annotate the implementation classes. All implementation classes should deserialize themselves, otherwise it will use the deserializer from the parent class which leads to a StackOverflowError.