Camel过滤器不工作

toiithl6  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(211)

我有这条 Camel 路线:

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过滤此响应,为此我在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
有什么问题吗?
谢谢你,谢谢你

smdnsysy

smdnsysy1#

我想您误解了Filter EIP的含义:它根据 predicate 过滤消息,因此,在您的示例中,当交换的内容与 jsonpath predicate 匹配时,消息将通过下一步。
你有不同的方式来达到你想要的,即

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

相关问题