I try to publish a message on a Queue with RabbitTemplate (using Spring Boot) and I got this message. I already tried to search for a solution.
Caused by: java.lang.IllegalArgumentException: SimpleMessageConverter only supports String, byte[] and Serializable payloads, received: com.example.demo.SimpleMessage
Maybe this part of code can help
@Override
public void run(String...strings) throws Exception {
SimpleMessage simpleMessage = new SimpleMessage();
simpleMessage.setName("FirstMessage");
simpleMessage.setDescription("simpleDescription");
rabbitTemplate.convertAndSend("TestExchange", "testRouting", simpleMessage);
}
I appreciate any collaboration.
The problem is that your class SimpleMessage
does not implement Serializable
.
RabbitTemplate.convertAndSend
uses SimpleMessageConveter
to convert your message into an amqp message. However SimpleMessageConverter
requires that message to implement the interface Serializable
.
Your SimpleMessage
class should look like follows:
public class SimpleMessage implements Serializable {
... your code here
}
You can learn more about Serializable objects here.
There is another solution: use a different implementation of the MessageConverter instead of default SimpleMessageConverter.
For example, Jackson2JsonMessageConverter:
public RabbitTemplate jsonRabbitTemplate(ConnectionFactory connectionFactory, ObjectMapper mapper) {
final var jsonRabbitTemplate = new RabbitTemplate(connectionFactory);
jsonRabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(mapper));
return jsonRabbitTemplate;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With