package com.ankurm.rabbit; import org.springframework.amqp.support.converter.JacksonJsonMessageConverter; import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Spring Boot does not auto-configure a JSON message converter for RabbitMQ. The default * is {@code SimpleMessageConverter}, which handles {@code String}, {@code byte[]} and * {@code Serializable} and nothing else: * *
* IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and * Serializable payloads, received: com.ankurm.rabbit.OrderMessage ** *
Declaring one {@code MessageConverter} bean fixes both directions — the auto-configured * {@code RabbitTemplate} and the listener container factory both pick it up. * *
Note the class name. Spring AMQP 4.1 ships {@code Jackson2JsonMessageConverter} (Jackson 2) * and {@code JacksonJsonMessageConverter} (Jackson 3) side by side, exactly as Spring Kafka * ships {@code JsonSerializer} and {@code JacksonJsonSerializer}. Boot 4 is a Jackson 3 * application; pick the one without the 2. * * @see docs/01-the-on-ramp.md */ @Configuration(proxyBeanMethods = false) public class ConverterConfiguration { @Bean MessageConverter messageConverter() { return new JacksonJsonMessageConverter(); } }