Java Jackson deserialize abstract class child which implements interface

Viewed 298

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.

1 Answers

Actually, i forgot to purge queue in Rabbit, before changing the models, that's why i got deserialization error.

Related