convertandsend something other than a string

1,268 views
Skip to first unread message

jha...@lexmark.com

unread,
Mar 14, 2016, 1:03:13 PM3/14/16
to rabbitmq-users
Using RabbitMQ and Spring, I have been able to successfully send and receive string messages.
However, when I send an object that is a class composed of strings and numbers (a json class), the @RabbitListener issues the following exception when a message is received:

2016-03-14 12:46:03 [SimpleAsyncTaskExecutor-1] [31mWARN [0;39m [36mo.s.a.r.l.ConditionalRejectingErrorHandler [0;39m - Execution of Rabbit message listener failed.
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details
:

With cause of:
Caused by: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: Could not resolve method parameter at index 0 in method:

and suggestion of:           [@Payload param is required]]

Here is the send:
DocumentConversionModel createdHandler = template.convertSendAndReceive(EXCHANGE_NAME, '', documentHandler)

Here is the receiver:
@RabbitListener(queues = QUEUE_NAME)
DocumentConversionModel receive(DocumentHandler inHandler) { ...}

DocumentHandler is a class that contains json fields of strings and integers.

I am using Groovy and when I substitute "def documentHandler"  as the receiver params, there is no exception.  However, there is no class data available.
I played around unsuccessfully with @Payload that was suggested above. I also tried the suggestion at link: http://www.howtobuildsoftware.com/index.php/how-do/hum/java-json-rabbitmq-spring-boot-spring-amqp-spring-boot-rabbitmq-mappingjackson2messageconverter-custom-object-conversion but it failed with a compiler error.

I assume that the convertSendAndReceive is doing some type of conversion of the message, but I don't know to what.

Is there a sample available that shows how to send and receive objects other than strings or numbers?

Gary Russell

unread,
Mar 14, 2016, 1:11:25 PM3/14/16
to rabbitm...@googlegroups.com
Please ask questions about Spring AMQP on Stack Overflow (spring-amqp tag).

This group is for RabbitMQ itself (including the java client that Spring AMQP uses).

The default MessageConverter in the listener container is a SimpleMessageConverter which only handles String and Serializable payloads.

To decode json into domain objects, you need to configure a Jackson2JsonMessageConverter into the listener container factory, as described in the documentation [1].

If the source is a Spring AMQP app (e.g. RabbitTemplate with the same converter) the type information is provided in message headers. If not, you have to configure a ClassMapper or a type mapper into the converter to help it with the conversion (or provide the type information in the required headers.


--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gary Russell

unread,
Mar 14, 2016, 1:23:21 PM3/14/16
to rabbitm...@googlegroups.com
Since you are using a template on the sending side, simply add a Jackson2JsonMessageConverter to the template (setMessageConverter).
Reply all
Reply to author
Forward
0 new messages