Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects

org.springframework.boot:spring-boot-starter-amqp uses SimpleMessageConverter as its default MessageConverter implementation. This means it handles text-based content, byte arrays, and serialized Java objects.

In this article, we’ll see how to send an…


This content originally appeared on DEV Community and was authored by İbrahim Gündüz

org.springframework.boot:spring-boot-starter-amqp uses SimpleMessageConverter as its default MessageConverter implementation. This means it handles text-based content, byte arrays, and serialized Java objects.

In this article, we'll see how to send and receive plain Java objects as RabbitMQ messages using Jackson2JsonMessageConverter.

Implementation

If your project doesn't use spring-boot-starter-web, add the following package to ensure you get the jackson libraries with the version managed by Spring boot.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-json</artifactId>
</dependency>

Create a new Jackson2JsonMessageConverter bean to use as the message converter for messages being sent and received.

@Bean
public Jackson2JsonMessageConverter messageConverter() {
    return new Jackson2JsonMessageConverter();
}

Create a new RestTemplate bean that uses the previously defined Jackson2JsonMessageConverter to convert messages being sent to objects.

@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter messageConverter) {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(messageConverter);
    return rabbitTemplate;
}

And finally, create a new SimpleRabbitListenerContainerFactory that uses the message converter to enable Spring Boot to transform messages into plain Java objects in the listeners you create later.

@Bean
public SimpleRabbitListenerContainerFactory listenerContainerFactory(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter messageConverter) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setMessageConverter(messageConverter);
    return factory;
}

Now you can send and receive messages easily, as shown below.

PaymentEvent event = new PaymentEvent(
        "https://webhook.site/ecd3dc67-e32e-41d9-82ff-3505c0fd2ab9",
        "payment.completed",
        "8a7f70f6-912c-497e-802c-c7bde9e4bb33"
);

rabbitTemplate.convertAndSend(MessageBrokerBindingConfig.PAYMENT_EVENTS_EXCHANGE, "payment.events.completed", event);

@RabbitListener(queues = MessageBrokerBindingConfig.PAYMENT_EVENTS_QUEUE)
public void onMessage(PaymentEvent event) {
    //  TODO 
}

Thanks for reading.

Credits:


This content originally appeared on DEV Community and was authored by İbrahim Gündüz


Print Share Comment Cite Upload Translate Updates
APA

İbrahim Gündüz | Sciencx (2025-10-14T07:30:11+00:00) Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects. Retrieved from https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/

MLA
" » Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects." İbrahim Gündüz | Sciencx - Tuesday October 14, 2025, https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/
HARVARD
İbrahim Gündüz | Sciencx Tuesday October 14, 2025 » Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects., viewed ,<https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/>
VANCOUVER
İbrahim Gündüz | Sciencx - » Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/
CHICAGO
" » Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects." İbrahim Gündüz | Sciencx - Accessed . https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/
IEEE
" » Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects." İbrahim Gündüz | Sciencx [Online]. Available: https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/. [Accessed: ]
rf:citation
» Spring Boot AMQP: How to Convert Messages for Sending and Receiving Objects | İbrahim Gündüz | Sciencx | https://www.scien.cx/2025/10/14/spring-boot-amqp-how-to-convert-messages-for-sending-and-receiving-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.