骆驼过滤器不起作用



我有这条骆驼路线:

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/(中测试过滤条件,它可以工作:

标准

结果

可能出了什么问题?

多谢。

我想你误解了过滤器 EIP 的含义:它根据谓词过滤消息,所以,在您的情况下,当交换的内容与jsonpath谓词匹配时,消息就会飞过下一步。

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

  • 通过使用拆分 EIP,然后筛选出您需要的内容
  • 通过使用消息转换器 EIP

最新更新