0

在我的 Apache Camel 应用程序中,我有多个条件来检查 JSON 中是否存在键。我想减少样板代码,因此我需要将我Expression的 s 转换为Predicate.

我的代码与Expressions:

.choice()
    .when().jsonpath("$.score", true).to("direct:b")
    .when().jsonpath("$.points", true).to("direct:b")
    .otherwise().to("direct:c");

另请参阅:JSONPATH

我的代码与Predicates:

.choice()
    .when(PredicateBuilder.or(jsonpath("$.score", true), jsonpath("$.points", true))).to("direct:b")
    .otherwise().to("direct:c");

另请参阅:谓词

但这不起作用,因为没有suppressExceptions参数(请参阅 参考资料BuilderSupport#jsonpath)。不幸的是,也没有方法exists(请参阅 参考资料ValueBuilder)。

如何编写Predicate用于检查 JSON 中是否存在密钥的代码?

4

1 回答 1

0

此代码解决您的问题。

.choice()
    .when(PredicateBuilder.and(jsonpath("$[?(@.score)]"), jsonpath("$.score"))).to("direct:b")
    .when(PredicateBuilder.and(jsonpath("$[?(@.points)]"), jsonpath("$.points"))).to("direct:b")
    .otherwise().to("direct:c");
于 2021-08-20T20:41:43.870 回答