Fatal message conversion error;message rejected;it will be dropped or routed to a dead letter exchan

在使用rabbitmq的时候出现消息反序列化失败,如下异常:
Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured
Fatal message conversion error;message rejected;it will be dropped or routed to a dead letter exchan
经过定位分析,原因是在MQ消息的生产端,设置了序列化转换Jackson2JsonMessageConverter,而默认的序列化类为SimpleMessageConverter。且在消费端没有设置反序列化转换。

解决方法:
因为消息是json串,使用String接收参数,再使用json工具类转化为对象;

import com.alibaba.nacos.client.utils.JSONUtils;
import com.atguigu.rabbit.common.constant.MqConst;
import com.atguigu.yygh.sms.service.SMSService;
import com.atguigu.yygh.vo.sms.SmsVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SmsReceiver {

    @Autowired
    private SMSService smsService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_SMS, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_SMS),
            key = {MqConst.ROUTING_SMS}
    ))
    public void send(
            String json,//接收传输过来的内容
            Message message, Channel channel) {
        SmsVo smsVo = null;
        try {
            //把json串转化为对应的对象
            //JSONUtils 包名(com.alibaba.nacos.client.utils.JSONUtils)
            smsVo = (SmsVo) JSONUtils.deserializeObject(json, SmsVo.class);
            System.out.println(smsVo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
上一篇:自学笔记 (一)-- 注解 Annotation


下一篇:使用springboot + druid + mybatisplus完成多数据源配置