首页 > 解决方案 > 无法在 RabbitMQ 接收器中将 content_type 从 application/octet-stream 更改为 application/json

问题描述

我有一个简单的 RabbitMQ 源和接收器。我正在向具有以下属性的源队列发布一条消息:

content_type -> application/json

和一个 JSON 有效负载:

{
  "userId": 2,
  "customerId": 1,
}

RabbitMQ 接收器使用application/octet-stream而不是 JSON 来获取消息。

我尝试使用以下属性启动应用程序:

spring.cloud.stream.default.contentType=application/json

但这没有帮助。

流定义:

stream_1=rabbitSource: rabbit --queues=queue1 --password=p --host=h --username=u | sink: rabbit --exchange=ex --routing-key=rk --converter-bean-name=jsonConverter --password=p --host=h --username=u

如何将内容类型设置为application/json?参考指南似乎没有答案。

发布版本:

更新:

正如@SabbyAnandan 的答案所建议的那样,我现在正在运行:

dataflow:>stream create --name test123 --definition "rabbitSource: rabbit --queues=queue --password=p --host=rmq --username=u --spring.cloud.stream.bindings.output.contentType='application/json' | sink: rabbit --exchange=ex --routing-key=rk --converter-bean-name=jsonConverter --password=p --host=rmq --username=p"
Created new stream 'test123123'

dataflow:>stream deploy --name test123 --properties "app.rabbit.spring.cloud.stream.bindings.output.contentType='application/json'"
Deployment request has been sent for stream 'test123'

但是content_type还是一样的。

标签: javarabbitmqspring-cloud-streamspring-cloud-dataflow

解决方案


在 Rabbit-source 的标头中设置的默认内容类型是:content-type: application/x-java-serialized-object.

您可以通过传递来覆盖该行为:

1)--spring.cloud.stream.bindings.output.contentType='application/json'作为 Rabbit 源的内联属性。

2) 在部署流时,您可以通过--properties="app.rabbit.spring.cloud.stream.bindings.output.contentType='application/json'.

http://<APP-HOST>:<APP-PORT?/actuator/configprops部署后,您可以通过转到特定于应用程序的端点来确认该属性是否被覆盖。

java -jar..要解决所有这些问题,您可以在通过 SCDF运行应用程序之前独立运行应用程序(即)以确认行为。如果它独立工作,它也应该在 SCDF 中工作。


推荐阅读