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

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

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

经过定位分析,原因是在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();
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5435748.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存