Tag Archives: message rejected; it will be dropped or routed to a dead letter exchan

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

When rabbitmq is used, the message deserialization fails, with the following exception:
fatal message conversion error; message rejected; It will be dropped or routed to a dead letter exchange, if so configured

after location analysis, the reason is that the serialization conversion jackson2jsonmessageconverter is set on the production side of MQ messages, and the default serialization class is simplemessageconverter. And the deserialization conversion is not set on the consumer side.

Solution:
because the message is a JSON string, use string to receive parameters, and then use JSON tool class to convert it into an object;

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,//Receive the transmitted content
            Message message, Channel channel) {
        SmsVo smsVo = null;
        try {
            //Convert the json string into the corresponding object
            //JSONUtils pack name(com.alibaba.nacos.client.utils.JSONUtils)
            smsVo = (SmsVo) JSONUtils.deserializeObject(json, SmsVo.class);
            System.out.println(smsVo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}