I have a service that listens to a queue then maps it to a POJO.
But I'm always getting this error even after setting the @Configuration
of ObjectMapper
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new JavaTimeModule());
return mapper;
}
My POJO:
public class ResultDto {
private ZonedDateTime dateSent;
private ZonedDateTime dateDeliveryReceiptReceived;
public ResultDto() {}
}
I get this error:
Caused by: org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-08-03T11:02:51.044+0000')
Thanks in advance!
Thanks for those who answered.
With the help of team mate, we discovered that the spring cloud has its own object mapper. And not the ObjectMapper
directly. Since this DTO/POJO is to the message from a AWS SNS/SQS.
This should do:
@Bean
public MappingJackson2MessageConverter mappingJackson2MessageConverter(ObjectMapper objectMapper) {
MappingJackson2MessageConverter jacksonMessageConverter = new MappingJackson2MessageConverter();
jacksonMessageConverter.setObjectMapper(objectMapper);
jacksonMessageConverter.setSerializedPayloadClass(String.class);
jacksonMessageConverter.setStrictContentTypeMatch(true);
return jacksonMessageConverter;
}