You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用StreamBridge发送含方括号的String消息并绕过消息转换?

背景

参考以下示例代码:

streamBridge.send("supply-out-0", (Object) "[hello world]", MediaType.TEXT_PLAIN);

通过上述代码,我向流绑定发送了一个非常简单的纯文本String类型数据。

请注意消息内容中特意添加的方括号(即[、])。


问题

由于方括号的存在,代码执行失败并抛出以下错误:

java.lang.IllegalStateException: Failed to convert. Possible bug as the conversion probably shouldn't have been attampted here
    at org.springframework.cloud.function.json.JacksonMapper.doFromJson(JacksonMapper.java:70)
    at org.springframework.cloud.function.json.JsonMapper.fromJson(JsonMapper.java:94)

即使配置了绑定级别的content-type,依然会出现相同错误:

spring:
  cloud:
    stream:
      bindings:
        supply-out-0:
          destination: xxx
          binder: xxx
          content-type: text/plain
# ... 省略其他配置 ...

问题与思考

我认为这可能是一个Bug,后续会在GitHub上提交报告。

目前我需要找到修复方案(修改实现代码,而非消息内容),请问有人知道如何解决吗?

目前我了解到,supply-out-0这个绑定名称是由Supplier函数名、-out-以及索引(单输出时始终为0)拼接而成。

该函数每秒被调用一次,但由于它不接收参数,我无法手动调用它(比如通过HTTP请求端点调用)并传入输入消息。

@Bean
public Supplier<String> supply() {
    return () -> "Hello World";
}

因此,我觉得StreamBridge是手动触发Spring Cloud Stream向消息主题发送消息的唯一方式,但遗憾的是它未按预期工作。


依赖版本

依赖版本
spring-cloud-dependencies2021.0.3
spring-boot-dependencies2.7.1
spring-cloud-stream已尝试3.2.4(托管版本)和3.2.6(覆盖版本)

其他说明

注:上述代码中的类型转换是必须的,用于解决方法调用歧义导致的编译错误😅。

  • StreamBridge#send(String, Object, MimeType) 👈
  • StreamBridge#send(String, String, Object)

内容的提问来源于stack exchange,提问作者blackr1234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:11:06