Search code examples
spring-amqpspring-rabbit

Spring RabbitMq - Apply custom payload converter


How do I apply a custom payloadConverter in MessagingMessageConverter. It defaults to the SimpleMessageConverter class.

I can easily apply a messageConverter onto the rabbitTemplate, but it only affects the outgoing message.

The MessagingMessageConverter belongs to the MessagingMessageListenerAdapter.

Do I need to create a containerFactory in order to apply a payloadConverter?


Solution

  • Yes, you set the converter on the container factory; you can either define your own bean or modify the one created by Boot. I assume you are using boot because otherwise you would have already had to define a factory.

    @Component
    class ContainerCustomizer {
    
        public ContainerCustomizer(AbstractRabbitListenerContainerFactory<?> factory) {
            factory.setMessageConverter(...);
        }
    
    }