I have an interface:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ChildA.class, name = "childA"),
@JsonSubTypes.Type(value = ChildB.class, name = "childB"),
})
public interface Event {
}
And then abstract class which implements this interface:
public abstract class SpecificEvent implements Event {
private ZonedDateTime timestamp;
}
And finally children of abstract class:
public class ChildA extends SpecificEvent {
}
public class ChildB extends SpecificEvent {
}
Jackson fails to deserialize children with error:
Could not resolve type id 'childA' as a subtype of
packages.SpecificEvent.
What am i doing wrong?
UPD
I'm consuming messages of children throw RabbitMQ. Rabbit config:
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new Jdk8Module());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.setDateFormat(StdDateFormat.instance
.withTimeZone(TimeZone.getTimeZone(ZoneId.of("Europe/Moscow")))
.withLocale(new Locale("ru"))
);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter(objectMapper());
}
For sending events i just use RabbitTemplate:
rabbitTemplate.convertAndSend(
exchange.getName(),
event.getEventRouteKey().getKey(),
event
);
And listen to it via @RabbitListener.