2.2 KiB
1. The on-ramp
Two dependency decisions decide whether anything works, and neither produces a helpful error.
Use the starter, not spring-rabbit
Boot 4 moved every auto-configuration out of spring-boot-autoconfigure into a per-technology
module. RabbitMQ's lives in spring-boot-amqp, package
org.springframework.boot.amqp.autoconfigure, and a bare org.springframework.amqp:spring-rabbit
dependency does not bring it. You get no RabbitTemplate, no RabbitAdmin, no listener
container factory — and a context that starts cleanly.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Boot does not give you a JSON converter
Spring Kafka defaults to serializers you configure. Spring AMQP defaults to
SimpleMessageConverter, which handles String, byte[] and Serializable and refuses
everything else:
IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and
Serializable payloads, received: com.ankurm.rabbit.OrderMessage
One bean fixes both directions, because the auto-configured RabbitTemplate and the listener
container factory both look for a MessageConverter:
@Bean
MessageConverter messageConverter() {
return new JacksonJsonMessageConverter();
}
Note the class name. Spring AMQP 4.1 ships Jackson2JsonMessageConverter (Jackson 2) and
JacksonJsonMessageConverter (Jackson 3) side by side — exactly as Spring Kafka ships
JsonSerializer and JacksonJsonSerializer. Boot 4 is a Jackson 3 application. The rule across
both stacks is the same: if the class name contains a 2, it belongs to the previous major
version of Jackson.
Versions
Boot 4.1.1 manages Spring AMQP 4.1.1 and com.rabbitmq:amqp-client 5.30.0. Maven Central
has amqp-client 5.35.0; overriding the managed version to reach it is a change you should have a
reason for.
One API change to know about: RabbitAdmin.QUEUE_MESSAGE_COUNT now holds a Long. Every older
example casts it to Integer, which is a ClassCastException at runtime and not at compile
time.