By default, the message sent by RabbitMQ is converted to bytecode. Here's how to send JSON data.
ObjectMapper
The easiest way to send JSON data is to use JSON tool classes such as ObjectMapper to convert the object to JSON format, and then send it. As follows:
@Autowired private ObjectMapper objectMapper; public void sendOrder(Order order) { try { String orderJson = objectMapper.writeValueAsString(order); Message message = MessageBuilder .withBody(orderJson.getBytes()) .setContentType(MessageProperties.CONTENT_TYPE_JSON) .build(); this.rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_ORDERS, message); } catch (JsonProcessingException e) { e.printStackTrace(); } }
Send JSON message
But it's cumbersome to write like this in every place where messages are sent. If you specify that the message format is JSON, you can use org.springframework.amqp.support.converter.Jackson2JsonMessageConverter as the default message converter.
To add a converter to RabbitConfig:
package com.lyt.rabbit.rabbitproducer; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) { final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMessageConverter(producerJackson2MessageConverter()); return rabbitTemplate; } @Bean public Jackson2JsonMessageConverter producerJackson2MessageConverter() { return new Jackson2JsonMessageConverter(); } }
In the management side of RabbitMQ, it can be seen that the converted json is indeed, and the specific types of objects are included in the headers
Receive JSON message
In order to receive JSON data, we should customize the RabbitMQ configuration by implementing RabbitListenerConfigurer.
package com.lyt.rabbit.rabbitcustomer; import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer; import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory; import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; @Configuration public class RabbitConfig implements RabbitListenerConfigurer { @Override public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) { registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory()); } @Bean MessageHandlerMethodFactory messageHandlerMethodFactory() { DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory(); messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter()); return messageHandlerMethodFactory; } @Bean public MappingJackson2MessageConverter consumerJackson2MessageConverter() { return new MappingJackson2MessageConverter(); } }