骆驼选择与json

时间:2013-09-16 09:23:00

标签: json xpath apache-camel fuseesb

我写了骆驼选择,路由输入是xml。如下所示: 我想用json作为输入编写选择,以便如何评估json以路由到下一个组件。请指导我。 JSON是:

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

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

4 个答案:

答案 0 :(得分:0)

您可以使用标头来评估表达式。在评估路径中的选择之前,您可以在处理器内设置标题(在输出消息中)。然后评估'simple'标签内的表达式。

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

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

答案 1 :(得分:0)

我不确定您是否可以在JSON上使用xpath。至少使用您正在使用的组件。 我做的是定义“xmljson”数据formar并使用它来解组JSON。 JSON到XMl然后在XML上应用xpath。

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

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

pom文件

<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>

答案 2 :(得分:0)

将来会有一张JIRA票据来查找解决方案,并使用&#34; json语言&#34;在Camel中开箱即用,因此您可以在表达式/谓词中使用它。

虽然我们还没有发现银弹,因为缺少一些支持表达式语法的好json库。虽然对JIRA票有一些想法,但是如何以不同的方式做到这一点:

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

答案 3 :(得分:0)

可能为时已晚,但答案是使用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>

不要忘记Maven依赖:

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