我使用以下代码进行消息转换:
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, Queue queue,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queue.getName());
container.setMessageListener(listenerAdapter);
container.setMessageConverter(new Jackson2JsonMessageConverter());
return container;
}
我的听众被宣布:
public void receiveMessage(List<Map<String, Object>> message) {
try {
System.out.println("Received <" + new String(message, "UTF-8") + ">");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
但它总是试图给出跟随错误:
Failed to invoke target method 'receiveMessage' with argument type = [class [B], value = [{[B@40c2d9c5}]","at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:408)
它似乎尝试调用byte []作为参数,而不是将json字符串转换为List&gt;。
答案 0 :(得分:1)
转换器需要content_type
消息属性,其中包含令牌json
- 例如application/json
。如果您使用的是至少1.6.1版本,则应该看到WARN日志。
log.warn("Could not convert incoming message with content-type ["
+ contentType + "], 'json' keyword missing.");
如果您无法更改生产者以正确设置内容类型,则可以对转换器进行子类化...
@Override
public Object fromMessage(Message message, Object conversionHint) throws MessageConversionException {
message.getMessageProperties().setContentType("application/json");
return super.fromMessage(message, conversionHint);
}