【问题标题】:How to write an exists predicate for jsonpath in Apache Camel?如何在 Apache Camel 中为 jsonpath 编写存在谓词?
【发布时间】:2021-08-20 07:57:18
【问题描述】:

在我的 Apache Camel 应用程序中,我有多个条件来检查 JSON 中是否存在键。我想减少样板代码,因此我需要将我的Expressions 转换为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");

另见:PREDICATES

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

如何编写 Predicate 来检查 JSON 中是否存在密钥?

【问题讨论】:

    标签: apache-camel json-path-expression


    【解决方案1】:

    这段代码解决了你的问题。

    .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");
    

    【讨论】:

    • 谢谢,不过现在又多了两个whens。我的目标是只使用一个when,因为我不想写多余的tos(在我的例子中,有一些代码行,而不仅仅是一个to)。你知道解决方案吗?也许将两者与PredicateBuilder.or结合起来?
    • 你可以像 stackoverflow.com/a/9273050/8916556 那样包含谓词
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多