Search code examples
javaspring-bootrabbitmqamqp

java.lang.IllegalArgumentException when publishing a message with RabbitTemplate


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.


Solution

  • 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.