I have pojo objects with inheritance and generics like this:
child object:
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MessageCreatedEvent extends AbstractEvent<MessageDto> {
@JsonCreator
public MessageCreatedEvent(MessageDto data) {
super(data);
}
}
parent:
@Data
public abstract class AbstractEvent<T> {
private final UUID id = UUID.randomUUID();
private T data;
public AbstractEvent(T data) {
this.data = data;
}
}
and content holding object:
@Data
public class MessageDto implements Serializable {
private UUID id;
private String content;
// and other fields
}
and jackson configuration which is used in rabbitTemplate:
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
At start I didn't use @JsonCreator
property but when I receive json message from RabbitMQ and tried it deserialize in rabbit handler I got this error:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
MessageCreatedEvent
(no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
After this I added @JsonCreator
but then properties in MessageDto object are not set. There is only id field filled and others are null.
Can you tell me what I have wrong configured? Thank you.
EDIT:
I try modified pojos and remove generic data field from parent, and move it into child, now deserialization working, so it looks like that Jackson has som problem with generics. Any idea?