骆驼滤镜不起作用

时间:2018-06-15 02:52:15

标签: filter apache-camel jsonpath jolt

我有这条骆驼路线:

 from("direct:myRoute")
      .id("myRoute")
      .setHeader("accept", constant("application/json"))
      .setHeader("Cache-Control", constant("no-cache"))
      .setHeader("content-Type", constant("application/json"))
      .setHeader(Exchange.HTTP_METHOD, constant("GET"))
      .setHeader("ID",constant("0072168580"))
      .removeHeader(Exchange.HTTP_PATH)
      .removeHeader("CamelHttp*")
      .setBody(simple("${null}"))
      .streamCaching()
      .to("http4" + URL)
      .to("jolt:customerSpec.json?inputType=JsonString&outputType=JsonString&contentCache=true")
      .log("Before: ${body}")
      .filter()
      .jsonpath("$.[?(@.customerId == '${header.ID}')]")
      .log("After: ${body}");

我通过http4消费的服务返回一个用jolt转换的响应,到目前为止没问题。 JSON转换结果是:

[
  {
    "customerId": "0072168580",
    "documentId": "IDO"
  },
  {
    "customerId": "0072168580",
    "documentId": "ID2"
  },
  {
    "customerId": "0072168580",
    "documentId": "CDO"
  },
  {
    "customerId": "0072172460",
    "documentId": "IDO"
  },
  {
    "customerId": "0072172460",
    "documentId": "ID2"
  },
  {
    "customerId": "0072197658",
    "documentId": "IDO"
  },
  {
    "customerId": "0072197658",
    "documentId": "ID2"
  },
  {
    "customerId": "0072197658",
    "documentId": "CDO"
  }
]

转换后的日志显示:

INFO myRoute - Before: [{"customerId": "0072168580","documentId": "IDO"},{"customerId": "0072168580","documentId": "ID2"},{"customerId": "0072168580","documentId": "CDO"},{"customerId": "0072172460","documentId": "IDO"},{"customerId": "0072172460","documentId": "ID2"},{"customerId": "0072197658","documentId": "IDO"},{"customerId": "0072197658","documentId": "ID2"},{"customerId": "0072197658","documentId": "CDO"}]

然后,我想通过customerId过滤此响应,我在标题中设置了一个值来执行此操作:

.jsonpath("$.[?(@.customerId == '${header.ID}')]")

显然,jsonpath表达式没问题,因为日志显示我有符合过滤条件的元素:

...
[main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['customerId']
[main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['customerId']
[main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: @['customerId']
[main] DEBUG org.apache.camel.processor.FilterProcessor - Filter matches: true for exchange: Exchange[ID-XYZ-1529020843413-0-1]

但是,过滤后的日志显示了相同的JSON,没有过滤它:

INFO myRoute - After: [{"customerId": "0072168580","documentId": "IDO"},{"customerId": "0072168580","documentId": "ID2"},{"customerId": "0072168580","documentId": "CDO"},{"customerId": "0072172460","documentId": "IDO"},{"customerId": "0072172460","documentId": "ID2"},{"customerId": "0072197658","documentId": "IDO"},{"customerId": "0072197658","documentId": "ID2"},{"customerId": "0072197658","documentId": "CDO"}]

我一直在测试在线工具中的过滤条件,例如http://jsonpath.com/,它可以运行:

Criteria

Results

可能出现什么问题?

非常感谢。

1 个答案:

答案 0 :(得分:3)

我认为您误解了Filter EIP的含义:它根据谓词过滤消息,因此,在您的情况下,当交换内容与 jsonpath 谓词匹配时,消息会飞过下一步。

你有不同的方式来实现你想要的,即

  • 使用Split EIP然后过滤掉您需要的内容
  • 使用Message Translator EIP