Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using RabbitMQ to send messages to a queue and in a consumer I get it, but I cannot cast it.

When I send it, I do this.

rabbitTemplate.convertAndSend("myExchange", "binding", MyObject);

MyObject is a custom objected created in that project. In another project, I created MyObject.class exactly the same (but I know the object signature is not the same).

The converted set for my listener is the following.

Jackson2JsonMessageConverter jsonMessageConverter() {
    Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
    messageConverter.setClassMapper(classMapper());
    return messageConverter;
private ClassMapper classMapper() {
    DefaultClassMapper classMapper = new DefaultClassMapper();
    classMapper.setDefaultType(MyObject.class);
    return classMapper;

So, at my listener, the following code doesn't work. I've noticed the body comes with [B@1232(byte[232].

@Override
public void onMessage(final Message message) {
    final MyObject myObject = (MyObject) messageConverter.fromMessage(message);

How do I achieve this?

You should be getting a ClassCastException. Is that right? Could you post the complete stack trace? – Brian Oct 28, 2016 at 18:42

Put the MyObject class in a seperate jar. Reference this new jar from both the sender and the recipient.

Additional Info
String is an object. Convert your object to json (in a string) and pass the jsonString as the third parameter to convertAndSend.

In the recipient you should do something like this:

final String jsonString = (String) messageConverter.fromMessage(message);
                not sure.  you could convert the object to json, then send json as the message (as a string) then on the other end, get the string and convert it from json to a locally defined, but with the same members class.
– DwB
                Oct 23, 2016 at 2:26
                example of what?  sending the json as a message?  implementing two classes that produce (and are produced from) the same json?
– DwB
                Oct 24, 2016 at 16:51

Ensure that you set the MessageConverter at the sender side to Jackson2JsonMessageConverter if you want to consume the message at the receiver side as json.

Jackson2JsonMessageConverter messageConverter = ...; // create and configure
rabbitTemplate.setMessageConverter(messageConverter);
rabbitTemplate.convertAndSend("myExchange", "binding", MyObject);

Otherwise the default SimpleMessageConverter is used to send the message.

The default converter is a SimpleMessageConverter, which is able to handle byte arrays, Strings, and Serializable Objects depending on the message content type header.

So I guess your MyObject is Serializable and the default MessageConverter serializes it to an byte array. As you said

So, at my listener, the following code doesn't work. I've noticed the body comes with [B@1232(byte[232].

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.