我的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 [],但结果是相同的。我确定我缺少简单的东西-只是不确定它是什么!
答案 0 :(得分:1)
Jackson2JsonMessageConverter
仅在邮件的content_type
标头包含json
的情况下执行转换。
否则,它将返回byte[]
。
byte[]
也不会转换为Byte[]
。设置标题或使用byte[]
。