el-chico
el-chico

Reputation: 155

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.

Upvotes: 10

Views: 13585

Answers (4)

Lisen Saka
Lisen Saka

Reputation: 56

The problem is with the object that you are trying to convert and send it via convertAndSend() method provided by rabbitTemplate, so in my example is like so:

"rabbitTemplate.convertAndSend("", "user-registration", userRegistrationRequest);"

here I am trying to convert and pass UserRegistrationRequest object, the objectUserRegistrationRequest used on the method does not implement the Serializable interface, so only by implementing the Serializable interface, the error was vanished.

Upvotes: 0

Victoria Agafonova
Victoria Agafonova

Reputation: 2178

In my case changing rabbitTemplate helped. First I had this one:

@Bean
public RabbitTemplate rabbitTemplate() {
    return new RabbitTemplate(connectionFactory());
}

And then I've changed it to this one:

@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(messageConverter);
    return rabbitTemplate;
}

@Bean
public MessageConverter messageConverter(ObjectMapper jsonMapper) {
    return new Jackson2JsonMessageConverter(jsonMapper);
}

Upvotes: 1

Evgene Krylov
Evgene Krylov

Reputation: 61

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;
}

Upvotes: 6

Dylan
Dylan

Reputation: 1395

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.

Upvotes: 25

Related Questions