简体   繁体   中英

Jackson don't deserialize properties value

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?

  • I copied your classes in a sample spring boot app (I am not using Lombok but replicating its behaviour) and doing the following and is working
    public class Testing {

      public static void main(String[] args) throws 
                  JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        MessageDto data = new MessageDto(UUID.randomUUID(), 
                                         "someContent");
        MessageCreatedEvent createdEvent = new MessageCreatedEvent();
        createdEvent.setData(data);
        String json = objectMapper.writeValueAsString(createdEvent);
        System.out.println(json);
        MessageCreatedEvent fromJson = 
            objectMapper.readValue(json, MessageCreatedEvent.class);
        System.out.println(fromJson.getData().getContent());
        System.out.println(fromJson.getData().getId());
      }
    }
  • Added a no argument constructor to MessageDto

  • Added a no argument constructor to AbstractEvent

  • Removed the following and is empty class:

     @JsonCreator public MessageCreatedEvent(MessageDto data) { super(data); }

I'm not sure if it can help, but you may need to use @JsonProperty . To be more specific, please refer to https://thepracticaldeveloper.com/2016/10/23/produce-and-consume-json-messages-with-spring-boot-amqp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM