Spring Boot / RabbitMQ different content_type with Jackson2JsonMessageConverter

2k Views Asked by At

I have a usecase where a service consumes different RabbitMQ queues.

  • On one of the queues there is JSON encoded data, where the content_type header is set to application/json
  • There is a second queue, where binary data is being consumed, content_type is application/octet-stream

In the configuration there is a MessageConverter defined:

@Bean
public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
}

Consuming methods are declared like that:

@RabbitListener(queues = "rpc-device-cmd")
public byte[] rpcCommand(byte[] request) throws IOException, ConfigurationException { .... }

My issue is that Jackson2JsonMessageConverter complains about the application/octet-stream header ( Could not convert incoming message with content-type application/octet-stream 'json' keyword missing.) and even worse, it is encoding the byte[] response of the method above to JSON and base64.

Question:
How is it possible to make sure that the JSON converter does not touch my byte[] responses and ignores messages with a "non-json" content_type?

1

There are 1 best solutions below

0
On BEST ANSWER

You achieve this by creating your own MessageConverter implementation to handle the rabbitMQ response string like as shown in the below example

https://github.com/nidhishkrishnan/messaging - here I have demonstrated how we can marshall and convert the RabbitMQ message string to Java domain object based on the binding key using MessageConverter

enter image description here

In your case you can retrieve the content-type from your message to marshal the output as per your requirement

String contentType = message.getMessageProperties().getContentType();