0

在 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 -> 丰富器 -> 请求通道 -> 服务激活器 -> 丰富器 -> 出站适配器?

4

1 回答 1

1

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

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

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

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

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

更新

这是一个示例如何使用 Java DSL 和 Spring Boot:https ://github.com/artembilan/sandbox/tree/master/spring-integration-enricher

于 2019-10-02T17:41:20.557 回答