【问题标题】:Spring Integration enrich/transform message using Rest callSpring Integration 使用 Rest 调用丰富/转换消息
【发布时间】:2019-10-02 16:16:22
【问题描述】:

在 Spring Integration 中,我收到如下消息:

{
  "name":"House",
  "attributeIds": [1,3,5]
}

我需要使用一些 Rest Service 来丰富/转换此消息,这将为我提供属性值。

例如http://restservice.com/attributes?id=1,3,5 会回复我

{"attributes": [
  {"id": 1, "value":"Waterproof"},
  {"id": 3, "value":"SoundProof"},
  {"id": 5, "value":"Concrete"}
]}

最终的对象应该是这样的:

{
  "name":"House",
  "attributes": [
    {"id": 1, "value":"Waterproof"},
    {"id": 3, "value":"SoundProof"},
    {"id": 5, "value":"Concrete"}
  ]
}

如何做到这一点?

应该是这样吗? https://www.youtube.com/watch?time_continue=273&v=DHPsWDgEUXg

InboundAdapter -> Enricher -> Request Channel -> Service Activator -> Enricher -> Outbound Adapter?

【问题讨论】:

    标签: spring-integration spring-integration-dsl spring-integration-http


    【解决方案1】:

    这确实是Content Enricher 的典型任务。

    因此,您需要将传入的 JSON 反序列化为普通的 Map。使用request-payload-expression="payload.attributeIds"ids 列表作为子流请求的有效负载。

    request-channel 上的订阅者可能只是简单的 Spring Integration HTTP Outbound Gateway 调用该 REST 服务并返回 attributes 消息。 这个网关可以在没有output-channel 的情况下通过replyChannel 标头将其结果返回到content-enricher

    当此回复消息到达content-enricher 时,可以使用简单的<int:property name="attributes"> 来填充请求Map 中的新选项。

    之后,您可以从该映射中删除 attributeIds 键,并在需要时将其序列化回 JSON

    更新

    以下是如何使用 Java DSL 和 Spring Boot 实现的示例:https://github.com/artembilan/sandbox/tree/master/spring-integration-enricher

    【讨论】:

    • 如果不是太多,你能举个例子吗?Java DSL 会是什么样子?可能不工作,但近似。可以使用一个 IntegrationFlow 完成吗?
    • 是的,可以。今天我会为你准备一些 Boot 应用程序
    • 请在我的回答中找到更新。
    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多