kamer
kamer

Reputation: 53

Adding Jackson KotlinModule to Micronaut JMS

I created a JMSListener which listens an AWS SQS queue. I receive message successfully but I cannot deserialize received message to a class without default values.

This is my queue listener function:

@Queue(value = "queuename", concurrency = "1-10", acknowledgeMode = JMSContext.CLIENT_ACKNOWLEDGE)
fun receive(@MessageBody sqsMessage: SQSMessageDto) {
    ....
}

...and class:

class SQSMessageDto(
    val notificationType: String,
    val mail: Mail,
    val receipt: Receipt
)

Function cannot parse deserialize text to SQSMessageDto unless I give default values for fields.

What I tried?

It works when I add a breakpoint in debugger mode on io.micronaut.jms.serdes.DefaultSerializerDeserializer and register Jackson's KotlinModule manually. (OBJECT_MAPPER.registerModule(new KotlinModule())) But I don't know how to make it properly.

Error Message:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.example.sqs.SQSMessageDto (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{.......}"]

Upvotes: 0

Views: 849

Answers (1)

IEE1394
IEE1394

Reputation: 1261

just use a BeanCreatedEventListener<ObjectMapper> like shown here: https://stackoverflow.com/a/53195504/7776688

@Singleton
static class ObjectMapperBeanEventListener implements BeanCreatedEventListener<ObjectMapper> {

    @Override
    public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
        final ObjectMapper mapper = event.getBean();
        mapper.registerModule(new KotlinModule())
        return mapper;
    }
}

Upvotes: 1

Related Questions