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 use following code for message converter:

SimpleMessageListenerContainer container(ConnectionFactory  connectionFactory, Queue queue,
        MessageListenerAdapter listenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(listenerAdapter);
    container.setMessageConverter(new Jackson2JsonMessageConverter());
    return container;

My listener is declared:

public void receiveMessage(List<Map<String, Object>> message) {
    try {
        System.out.println("Received <" + new String(message, "UTF-8") +     ">");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();

But it always try to gives follow error:

Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@40c2d9c5}]","at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:408)

It seems it tries to invoke byte[] as argument instead of convert json string to List>.

The converter requires a content_type message property that contains the token json - e.g. application/json. You should see a WARN log if you are using at least version 1.6.1.

log.warn("Could not convert incoming message with content-type ["
        + contentType + "], 'json' keyword missing.");

If you can't change the producer to set the content type properly, you can subclass the converter...

@Override
public Object fromMessage(Message message, Object conversionHint) throws MessageConversionException {
    message.getMessageProperties().setContentType("application/json");
    return super.fromMessage(message, conversionHint);
                It's not clear what you don't understand; I said create a subclass of Jackson2JsonMessageConverter and override that method as shown. It's rude to down-vote a clear answer.
– Gary Russell
                Oct 20, 2020 at 21:10
        

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.