json - 如何从消息头中获取我的主题名称并发布
问题描述
我是弹簧集成的新手。我正在尝试构建一个 spring rest 服务,它将获取带有一些 json 消息的任何 HTTP 请求并发布到 kafka 主题。
我的 json 消息将通过 RequestBody 发布,其中将在消息头中包含主题名称。
我可以将消息从我的控制器发布到 kafka 频道,但是我很难从我的 json 消息头中获取主题名称。
任何人都可以建议一种从我的消息头中获取主题名称的方法(通常一个 HTTP 请求包含一个带有主题名称的 json 消息)并使用该主题来发布消息。
我的 json :
{"resourceType": "MessageHeader",
"topicName": "testToptic",
"messagePayload":{
"location": "chennai",
"messageDetail": {
"department-id": 123,
"department-name": "SSS",
"pincode": 600009
}
}
}}
这是我的 bean 和处理程序
@Bean
public IntegrationFlow hanldeGenericKafka() {
return IntegrationFlows.from(sendToKafkaChannel)
.handle(
kafkaGenericMessageHandler(producerFactory),
e -> e.id("kafkaProducer2"))
.get();
}
public KafkaProducerMessageHandlerTemplateSpec<String, String> kafkaGenericMessageHandler(
ProducerFactory<String, String> producer) {
return Kafka
.outboundChannelAdapter(producer)
.sync(true)
.headerMapper(kafkaDefaultHeaderMapper())
.messageKey(m -> m.getHeaders()
.get("topicname"))
.configureKafkaTemplate(t -> t.id("kafkaTemplate"));
}
解决方案
您可以使用带有内置JsonPath SpEL 函数的表达式从 JSON 有效负载中提取字段值。
使用适配器中的表达式.topicExpression()
。
推荐阅读
- github - Github repo url 显示 readme.md 而不是 index.html
- windows - Appveyor 是否支持在 Windows 10 专业版和家庭版上进行测试?
- core-data - 为通用 NSFetchRequest 方法传递参数
- javascript - 邮递员正则表达式 - SyntaxError:无效或意外的令牌
- docker - Azure Devops Server 自托管 docker 代理设置 SSL 错误
- kubernetes - 在 Keycloak 和 Kubernetes 中正确管理用户会话
- java - 为什么 Eclipse 2020-09 中的 WindowBuilder 编辑器中的设计选项卡为空?
- python - 如何在 tkinter 的组合框中设置默认值?
- antlr - 如何在解析规则中获取标记的行?
- mongodb - 如何为在 kubernetes 集群上运行的 Kafka Connect 配置 MongoDB 官方源连接器