I am using jackson as part of serializing and deserializing in my project (Spring Java).
In normal scenarios where I have interface(contract) acting as field in POJO,
then I use @JsonTypeInfo
and @JsonSubTypes
to achieve deserialization in polymorphic cases.
But, right now, I have scenariio something like this:
public class classA {
private contractA fieldA;
//constructor and getter-setters.
}
then,
public interface contractA {
}
and finally,
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(/* concrete-class1 as name-value */),
@JsonSubTypes.Type(/* concrete-class2 as name-value */),
})
public interface contractB extends contractA {
//contract methods.
}
Now, when classA
is passed as controller request body and I pass fieldA
as concrete-class1
or concrete-class2
,
JsonSubTypes
are not being used by jackson to deserialize into one of them.
The reason why I did this and had two contracts is due to package dependencies. contractB
is in different package as of contractA
's.
How can I configure on contractA
using jackson that this class has its JsonSubTypeInfo
specified in its subclasses.
Or, any other libraries or approaches are also welcomed.
Thank you !