Question

I've written camel choice and input for route is xml. shown below: I want to write choice with json as an input so how to evaluate the json to route to next component. Kindly guide me. JSON is:

{
  "service": { "serviceType": "OtherServcie" }
}

<choice>
 <when>
  <xpath>/service/serviceType='PaymentServcie'</xpath>
  <log message="In PaymentServcie"/>
 </when>
 <otherwise>
  <log message="In OtherServcie"/>
 </otherwise>
</choice>
Was it helpful?

Solution

Maybe too late, but the answer is to use camel-jsonpath.

<route>
  <from uri="direct:start"/>
  <choice>
    <when>
      <jsonpath>$.service[?(@.serviceType=='PaymentService')]</jsonpath>
      <log message="In PaymentServcie"/>
    </when>
    <otherwise>
      <log message="In OtherServcie"/>
    </otherwise>
  </choice>

Don't forget the Maven dependency :

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jsonpath</artifactId>
    <version>${camel.version}</version>
</dependency>

OTHER TIPS

You can use header to evaluate expression. Just before evaluating the choice within a route, you can the set a header(in out message) within the processor. Then evaluate the expression within 'simple' tag.

${in.header.serviceType}=='PaymentService'

http://camel.apache.org/simple.html

I'm not sure you can use xpath on JSON. At least with the component you are using. What I do Is define "xmljson" data formar and use it to unmarshal JSON. JSON to XMl and then apply xpath on XML.

<camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint">
    <dataFormats>
           <xmljson id="xmljson"/>
    </dataFormats>

   <route 
    <from uri<.................
          ..........
          <unmarshal ref="xmljson"/>

pom file

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-xmljson</artifactId>
    <version>2.10.0.fuse-71-047</version>
    <!-- Use the same version as camel-core, but remember that this component 
        is only available from 2.10 onwards -->
</dependency>

There is a JIRA ticket to look into a solution in the future, having a "json language" out of the box in Camel so you can use it in expressions/predicates.

Though we haven't found the silver bullet yet, as there lacks some good json libraries that support an expression syntax. Though there is some thoughts on the JIRA ticket how to do this in a different way:

https://issues.apache.org/jira/browse/CAMEL-6238

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top