I have this Camel route:
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}");
The service I consume through http4 returns a response that is transformed with jolt, no problem so far. The JSON transformation result is:
[
{
"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"
}
]
The log after transformation shows:
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"}]
Then, I want filter this response by customerId, I am setting a value in header to do it:
.jsonpath("$.[?(@.customerId == '${header.ID}')]")
Apparently, the jsonpath expression is ok, because the log shows me there were elements met the filtering criteria:
...
[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]
However, the log after filtering shows me the same JSON, without filter it:
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"}]
I have been test the filter criteria in online tools, like http://jsonpath.com/ and it works:
What could be wrong?
Thanks a lot.
I think you misunderstand the meaning of Filter EIP: it filters a message according to a predicate so, in your case, as the content of the exchange matches the jsonpath predicate, the message flew through the next step.
You have different way to achieve what you want, i.e