消息转换器
当消息生产者发送的数据为对象时,接受到的信息如下,需要通过转换器将其转为易读、容量小的数据
![image-20241016上午101616219](/Users/mianbao/Library/Application Support/typora-user-images/image-20241016上午101616219.png)
配置JSON转换器
1.导入依赖
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.10</version>
</dependency>
2.在消息生产者和消费者的配置类或新建MessageConverterConfig类中添加Bean
@Bean
public MessageConverter messageConverter() {
Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
jackson2JsonMessageConverter.setCreateMessageIds(true);
return jackson2JsonMessageConverter;
}
3.生产者发送消息为对象,消费者使用对象接收即可
@Test
public void sendTopicMessage() {
String topicExchange = "topic.exchange";
User user = new User();
user.setName("debu");
user.setAge(18);
user.setHeight(230);
amqpTemplate.convertAndSend(topicExchange, "user.info", user);
}
@RabbitListener(queues = "topic1.queue")
public void getTopicMessage3(User user) {
log.info("消费者user号 收到消息:{}", user);
log.info("user的name是:{}",user.getName());
}
标签:info,jackson2JsonMessageConverter,RabbitMQ,发送,user,接收,public,User
From: https://www.cnblogs.com/superdebu/p/18469235