0

I am using Camel APIs for routing in my project with ESB and have the below requirement for a specific problem:-

There is a web service called using the form tag URI which takes in the request as JSON and responds the POST call with JSON data. I need to include the request JSON data as one of the fields inside my response JSON. I am searching for a possible way to enrich my message. I could do it by writing the transformation logic in a java class. But Constraint is that I can not use separate Java class for doing data transformation. I have to do it via configuration inside camelContext using tags. Please suggest.

Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
Deepak S
  • 1,544
  • 3
  • 15
  • 33
  • What do you mean by using tags, do you mean you cannot write Java code, but must only do XML configuration? – Claus Ibsen Sep 26 '17 at 14:15
  • @Claus Ibsen yes, preferably want to do it using XML configuration only , but if we have to use Java class to write the enrichment logic and no other option that would be ok. But could you tell which is better XML based or Java based if there exists any XML configuration for this ? – Deepak S Sep 28 '17 at 02:06

2 Answers2

0

You can read entire Json body as a tree JsonNode body = mapper.readTree(input); and after that enrich it with your construction. For example, if you have next json body:

{"valueList": {
    "key": "1",
    "value": "2"
  }
}

you can enrich it with this piece of code:

  ObjectMapper mapper = new ObjectMapper();
  ObjectNode rootNode = mapper.createObjectNode();
  ArrayNode headersList = mapper.createArrayNode();
  rootNode.set("headersList", headersList);
  rootNode.putPOJO("valueList", body.get("valueList"));
0

I think you need to make a look at setBody method. Please check here.

...
<setBody>
<simple>
{
"message" : "${body}"
}
</simple>
</setBody>
...
Jayarajan
  • 146
  • 6