代码示例:
1. 引入依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
2. 在启动类中创建Bean
package com.itheima;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ErpApplication {
public static void main(String[] args) {
SpringApplication.run(ErpApplication.class, args);
}
@Bean
public MessageConverter jacksonMessageConvertor() {
return new Jackson2JsonMessageConverter();
}
}
3. 测试:发送者发送消息给队列
@GetMapping("/mq05")
public void mq05(){
String queueName = "object.queue";
Map<String, Object> msg = new HashMap<>();
msg.put("name", "jack");
msg.put("age", 21);
rabbitTemplate.convertAndSend(queueName, msg);//队列名称、要发送的消息
}
对比:下面的是加了消息转换器的,上面的是没加的时候的
5. 消费者从队列中接收消息(跟之前一样)
@RabbitListener(queues = "object.queue")
public void listenQueue07(Map<String, Object> msg) {
System.out.println("消费者收到了object.queue的消息:" + msg);
}
标签:amqp,org,springframework,消息,import,msg,转换器,RabbitMQ,public From: https://www.cnblogs.com/gagaya2/p/17872358.html