Spring Boot集成RabbitMQ发送接收JSON
Posted 刘元涛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot集成RabbitMQ发送接收JSON相关的知识,希望对你有一定的参考价值。
默认情况下RabbitMQ发送的消息是转换为字节码,这里介绍一下如何发送JSON数据。
ObjectMapper
最简单发送JSON数据的方式是把对象使用ObjectMapper等JSON工具类把对象转换为JSON格式,然后发送。如下:
@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();
发送JSON消息
但是在每一个发送消息的地方都这样写就会很繁琐。如果规定了消息的格式为JSON,可以使用org.springframework.amqp.support.converter.Jackson2JsonMessageConverter作为默认的消息转换器。
在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();
在RabbitMQ的的管理端查看,转换后的确实是json,headers里包含了对象的具体类型等
接收JSON消息
为了接收JSON数据,我们应该通过实现RabbitListenerConfigurer来定制RabbitMQ配置。
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();
以上是关于Spring Boot集成RabbitMQ发送接收JSON的主要内容,如果未能解决你的问题,请参考以下文章
[SpringBoot] Spring Boot(13)RabbitMQ安装与集成
Spring boot集成RabbitMQ中Exchange与Queue参数详解