java.lang.IllegalArgumentException when publishing a message with RabbitTemplate
Asked Answered
C

4

10

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.

Chronogram answered 15/3, 2019 at 19:53 Comment(3)
somewhere SimpleMessageConverter.createMessage is being called. I can't remember if convertAndSend calls that for you or not.Roadside
Looking at the source for spring, convertAndSend, calls MessageConverter.toMessage which calls MessageConverter.createMessage and since this is an instance of SimpleMessage we get SimpleMessageConverter.createMessage.Roadside
Agree with what Dylan has mentioned below. You need to make your object as Serializable. Take a look at some samples on sending message similar to your use case here. thepracticaldeveloper.com/2016/10/23/…Mesocratic
R
25

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.

Roadside answered 15/3, 2019 at 20:30 Comment(0)
G
6

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;
}
Ganley answered 8/3, 2022 at 13:9 Comment(0)
C
1

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);
}
Corie answered 6/7, 2023 at 19:4 Comment(0)
R
0

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.

Recha answered 15/12, 2023 at 11:54 Comment(2)
Thanks a lot for your suggestion @Yunnosch, of course i will check out this and i will try to make my answers more clear in the future.Recha
just did it, so let me know if something still missing. Thanks a lot!Recha

© 2022 - 2025 — McMap. All rights reserved.