37 lines
1.4 KiB
Java
37 lines
1.4 KiB
Java
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 <b>not</b> 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:
|
|
*
|
|
* <pre>
|
|
* IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and
|
|
* Serializable payloads, received: com.ankurm.rabbit.OrderMessage
|
|
* </pre>
|
|
*
|
|
* <p>Declaring one {@code MessageConverter} bean fixes both directions — the auto-configured
|
|
* {@code RabbitTemplate} and the listener container factory both pick it up.
|
|
*
|
|
* <p>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 <a href="../../../../../docs/01-the-on-ramp.md">docs/01-the-on-ramp.md</a>
|
|
*/
|
|
@Configuration(proxyBeanMethods = false)
|
|
public class ConverterConfiguration {
|
|
|
|
@Bean
|
|
MessageConverter messageConverter() {
|
|
return new JacksonJsonMessageConverter();
|
|
}
|
|
|
|
}
|