Reputation: 2135
I'd like to process a subset of a JSON within an Apache Camel route. For example, given the following JSON message:
{
"text_1": "some text input",
"text_2": "some other text input"
}
I would like to extract a JSON string "some text input"
and POST
the value to a web service. Following this I would like to replace the existing text with the result. Let's assume the response from the web service was "some text output"
, then I would like to transform the JSON within a Camel route as follows:
{
"text_1": "some text output",
"text_2": "some other text input"
}
My understanding is the content enricher
and aggregator
patterns might be able to achieve this. Guidance would be appreciated.
Upvotes: 1
Views: 877
Reputation: 4919
Yes, Content Enricher EIP in combination with Message Translator EIP is a good choice.
You can do something similar to this:
from("direct:json")
.unmarshal(json)
.enrich("direct:callService", (original, response) -> {
original.getIn().getBody(Map.class).put(
"text_1",
response.getIn().getBody(String.class)
);
return original;
})
.marshal(json)
.to("log:result");
from("direct:callService")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setBody(simple("${body[text_1]}"))
.to("http4://httpbin.org/post");
Full example can be found here https://gist.github.com/bedlaj/aaa5c80ed8cc4c64308e7fbd1d7d57f1
Upvotes: 2