骆驼过滤器不起作用

Camel filter doesn't work

我有这条骆驼路线:

 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消费的服务returns一个用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 过滤此响应,我在 header 中设置一个值来执行此操作:

.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

有什么问题吗?

非常感谢。

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

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

  • 通过使用Split EIP,然后过滤出你需要的
  • 通过使用消息转换器 EIP