当前位置: 首页 > 知识库问答 >
问题:

Spring AMQP错误:无法使用传入消息调用侦听器方法

武琛
2023-03-14

我有课:

public class MqMessage implements Serializable{
    private String event;
    private String absolutePath;
    private boolean isDirectory;
    private Integer hash;
    private Node node;

    get/set

}

配置类:公共类RabbitConfiguration{

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory =
            new CachingConnectionFactory("localhost");
    return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setExchange("exchange-events");
    return template;
}

//объявляем очередь
@Bean
public Queue myQueue1() {
    return new Queue("queue-events");
}

@Bean
public FanoutExchange fanoutExchangeA() {
    return new FanoutExchange("exchange-events");
}

@Bean
public Binding binding1() {
    return BindingBuilder.bind(myQueue1()).to(fanoutExchangeA());
}
    public class ServerHandler implements EventHandler {

        //сама структура отражающая состояние файлов, содеражащая метоы для работы с ними
        @Autowired
        Node fileTreeRoot;

        SimpleMessageConverter simpleMessageConverter;

        @Override
        public void setRoot(Node fileTreeRoot) {
            this.fileTreeRoot = fileTreeRoot;
        }
        @Autowired
        RabbitTemplate rabbitTemplate;

        //логика обработки событий
        @Override
        public void eventHandle(String event, String path) {

            /*bussines-logick
*/
            rabbitTemplate.setExchange("exchange-events");

            rabbitTemplate.convertAndSend(new MqMessage(event,fileTreeRoot));
            return;
        }

        public ServerHandler() {

    }

听众:

public class Client {
Node rootNodeClient = new Node();
EventHandler handlerClient = new ClientHandler();


@RabbitListener(queues = "queue-events")
public void onMessage(MqMessage message) {
    System.out.println(message.getNode().hashCode());
    rootNodeClient = message.getNode();
}

a仅启动应用程序有错误

2017-08-08 12:58:02.128警告5024---[cTaskExecutor-1]S.A.R.L.ConditionalRejectingErrorHandler:Rabbit消息侦听器执行失败。

原因:org.SpringFramework.Messaging.Handler.Annotation.Support.MethodArgumentNotValideXception:无法解析public void prcjt.Client.Client.OnMessage(prcjt.message.MQMessage)中索引0处的方法参数:1错误:[对象“message”中的错误:codes[];arguments[];默认消息[有效负载值不能为空]]

错误并不总是存在,请帮助

共有1个答案

阚英武
2023-03-14
Caused by: org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException: Could not resolve method parameter at index 0

从异常信息来看,spring似乎无法正确解析侦听器中的MQMessage,您可以尝试向客户端添加一个MappingJackson2MessageConverter。请参阅此链接。

 类似资料: