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 !
This problem is later on solved by introducing our own custon JsonTypeInfo
.
When the application is under deployment, we fetch all subclasses which is present in the JsonTypeInfo
annotation (jackson like custom annotaion) and maintain a data-structure, that will be used while serializing
and deserializing
. This process is somewhat similar to the Jackson
one (in addition to lookup for nested hierarches as well).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.