【问题标题】:The spring-stream sends a message with "text/plain" content-type instead of "application/json"spring-stream 发送带有“text/plain”内容类型而不是“application/json”的消息
【发布时间】:2018-05-12 01:43:50
【问题描述】:

我想在我的队列上生成内容类型为 application/json 的响应,因为有效负载包含 json 对象。

但是……

不知道为什么我的 spring-stream(基于 RabbitMQ)总是生成带有以下标头的消息(有效负载是正确的并且具有 JSON 格式)。

(view from rabbit ui)
properties:
    delivery_mode:  2
    headers:    
      contentType:  text/plain
      originalContentType:  application/json;charset=UTF-8
    content_type:   text/plain

所以 content_type 等于 text/plain 而不是 application/json;charset=UTF-8

我尝试在以下配置中设置 content-type

spring:
  cloud:
    stream:
      default:
        content-type: application/json
      bindings:
        my-channel:
          content-type: application/json
          destination: my-channel-rs
          producer:
            exchange-type: direct
            max-length: 1000

但没有帮助。

我觉得在正确的响应中,单个标头内容类型应该存在值“application/json”。

但也许我错了?有人能够解释上述行为吗?

【问题讨论】:

    标签: spring-cloud-stream spring-rabbit


    【解决方案1】:

    我遇到了类似的问题。我需要底层的 spring-amqp 来使用“content-type”标头来设置实际的内容类型。 Spring-amqp 通过检查消息负载的类来计算它,而不考虑“内容类型”标头。

    我创建了一个可能会或可能不会被接受的拉取请求:https://github.com/spring-projects/spring-amqp/pull/763/commits/25e4f9adf523a802ac5f26dd373eff8bb764fb49

    【讨论】:

      【解决方案2】:

      看来您使用的是 1.3 版本的 S-C-Stream。能否请你确认?我们当然可以看看。 此外,FWIW,只要您的消费者是 SCSt 应用程序,整个“originalContentType”问题就不是问题(因为它将重置为您所期望的),但我假设它不是,对吗?

      此外,您有机会升级到 2.0.0.M3。围绕contentType 分辨率进行了很多改进。

      【讨论】:

      • originalContentType: application/json;charset=UTF-8 - rabbit binder 不适用于与任意 rabbitmq 应用程序通信。为此目的使用rabbit 接收器。
      • 1.是的,这是我的消费者不基于 s-c-stream 的第一种情况 :) 这是完全不同的技术,由其他人开发。 2. spring-cloud-stream 的版本是Chelsea.SR2 (spring cloud dependencies = Dalston.SR4)。 3.升级到2.0.0.M3?应用程序适用于生产。我宁愿等待附带spring cloud的spring boot 2正式发布。
      • @GaryRussell 你说我不能使用 spring-cloud-stream 作为不基于 spring-cloud-stream 的消费者的消息生产者? “兔子水槽”是什么意思?
      • 是的;这就是我所说的(至少在 2.0 之前)。 rabbit sink 是一个 spring-cloud-stream 应用程序,它从(任何)Spring Cloud Stream binder 接收消息并将消息发布到 rabbitmq。
      猜你喜欢
      • 2015-06-08
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 2010-12-25
      • 2023-03-08
      • 2016-08-30
      相关资源
      最近更新 更多