I have an interface -
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
interface Base { ... }
I have two derived classes - ClassA
and ClassB
. I am attempting to serialize and deserialize with Jackson ION to base type as follows -
class TestSerDeSer {
private static ObjectMapper MAPPER = new IonObjectMapper();
static {
MAPPER.registerSubtypes(new NamedType(A.class, "A"));
MAPPER.registerSubtypes(new NamedType(B.class, "B"));
}
public byte[] serialize(Base baseType) {
try {
return MAPPER.writeValueAsBytes(baseType);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public Base deserialize(byte[] bytes) {
Base base;
try {
base = MAPPER.readValue(bytes, Base.class);
return base;
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
I am creating an Object of Class A
and serializing and desrializing using above functions as
Base baseObj = new ClassA(...);
//serialization works fine
byte[] serializedBytes = serialize(baseObj);
//this line throws exception
Base deserializedBase = deserialize(serializedBytes);
The exception is -
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class mypackage.path.Base]: missing type id property 'type'
I am registering subtypes in ObjectMapper. I also have the annotation for type in base interface. What is it that I am missing here?