Search code examples
spring-integrationspring-integration-dslspring-integration-http

Spring Integration enrich/transform message using Rest call


In Spring Integration, I have message like following :

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

I need to enrich/transform this message using some Rest Service, which will give me the attribute values.

For example http://restservice.com/attributes?id=1,3,5 will answer me with

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

And the final object should look like this:

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

How can this be achieved?

Should it be like this? https://www.youtube.com/watch?time_continue=273&v=DHPsWDgEUXg

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


Solution

  • This is indeed a typical task for Content Enricher.

    So, what you need is to deserialize that incoming JSON into a plain Map. Use a request-payload-expression="payload.attributeIds" to have that list of ids as a payload for sub-flow request.

    A subscriber on the request-channel could be just simple Spring Integration HTTP Outbound Gateway to call that REST service and get an attributes message back. This gateway can just come without an output-channel to produce its result back into a content-enricher via replyChannel header.

    When this reply message comes to the content-enricher, a simple <int:property name="attributes"> can be used to populate that new option in the request Map.

    Afterwards you can remove an attributeIds key from that map and serialize it back to JSON if needed.

    UPDATE

    Here is a sample how it could be possible with Java DSL and Spring Boot: https://github.com/artembilan/sandbox/tree/master/spring-integration-enricher