У меня есть интерфейс -
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
interface Base { ... }
У меня есть два производных класса - ClassA
и ClassB
.Я пытаюсь сериализовать и десериализовать с помощью Jackson ION до базового типа следующим образом -
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);
}
}
}
Я создаю объект Class A
, а сериализацию и десериализацию с использованием вышеуказанных функций как
Base baseObj = new ClassA(...);
//serialization works fine
byte[] serializedBytes = serialize(baseObj);
//this line throws exception
Base deserializedBase = deserialize(serializedBytes);
Исключение -
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'
Я регистрирую подтипы в ObjectMapper.У меня также есть аннотация для типа в базовом интерфейсе.Что мне здесь не хватает?