我正在使用带有RabbitMQ活页夹的Spring Cloud Stream。它适用于byte[]
有效负载和Java本机序列化,但我需要使用JSON有效负载。
这是我的处理器类。
@EnableBinding(Processor.class)
public class MessageProcessor {
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public OutputDto handleIncomingMessage(InputDto inputDto) {
// Run some job.
return new OutputDto();
}
}
InputDto
和OutputDto
是带杰克逊注释的POJO。
答案 0 :(得分:8)
在您的消费者中,您可以添加内容类型配置,例如
spring.cloud.stream.bindings.input.content-type: application/x-java-object;type=my.package.InputDto
你也可以添加
spring.cloud.stream.bindings.output.content-type: application/json
强制传出消息有效负载为JSON(对于互操作等)。
请注意"输入"和"输出"是活页夹频道名称(即在您的应用中Processor
中定义)。
我认为这很有可能变得更容易或更自动化,但需要一些工程工作才能在Spring Cloud中实现这一目标。如果您想关注它,请在github中出现问题:https://github.com/spring-cloud/spring-cloud-stream/issues/156。
要手动向Spring Cloud Stream发送消息,您可以手动设置标题(但使用Stream更容易)。在Rabbit管理UI中,JSON消息如下所示:
priority: 0
delivery_mode: 2
headers:
contentType: text/plain
originalContentType: application/json
content_type: text/plain