Spring AMQP接收器的参数类型错误

时间:2018-09-26 21:38:24

标签: spring rabbitmq spring-amqp

我的Spring AMQP应用程序在启动时一直记录以下异常:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@660cff44}]

根据我的搜索,我了解这是因为消息类型与类不兼容?但是,我看不到它在哪里。

以下是相关的代码段:

@Bean
public MessageConverter jsonMessageConverter(){
    return new Jackson2JsonMessageConverter();
}

@Bean
Queue queue() {
    return new Queue(config.getAMQPResultsQueue(), false);
}

@Bean
TopicExchange exchange() {
    return new TopicExchange(config.getAMQPResultsExchange());
}

@Bean
Binding binding(Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with("#");
}

@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(config.getAMQPResultsQueue());
    container.setMessageListener(listenerAdapter);
    container.setMessageConverter(jsonMessageConverter());
    return container;
}

@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}

@Component
public class Receiver {
    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
    }
}

我尝试将消息类别设置为Byte [],但结果是相同的。我确定我缺少简单的东西-只是不确定它是什么!

1 个答案:

答案 0 :(得分:1)

Jackson2JsonMessageConverter仅在邮件的content_type标头包含json的情况下执行转换。

否则,它将返回byte[]

byte[]也不会转换为Byte[]。设置标题或使用byte[]