我試過註冊一個JsonMessageConverter,但無濟於事。
最好是看看你的企圖並找出我們的問題。
現在我只能說你應該爲發送和接收部分提供JsonMessageConverter
。
我剛剛測試了gs-messaging-rabbitmq
:
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
MessageConverter messageConverter;
.....
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver, MessageConverter messageConverter) {
MessageListenerAdapter adapter = new MessageListenerAdapter(receiver, "receiveMessage");
adapter.setMessageConverter(messageConverter);
return adapter;
}
@Bean
MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
.....
System.out.println("Sending message...");
rabbitTemplate.setMessageConverter(messageConverter);
rabbitTemplate.convertAndSend(queueName, new Foo("Hello from RabbitMQ!"));
凡Receiver
已被更改爲這個:
public void receiveMessage(Foo message) {
System.out.println("Received <" + message + ">");
latch.countDown();
}
所以,輸出是:
Waiting five seconds...
Sending message...
Received <Foo{foo='Hello from RabbitMQ!'}>
當我們使用Foo
這樣的:
@Override
public String toString() {
return "Foo{" +
"foo='" + foo + '\'' +
'}';
}
具有適當的獲取器和設置器。