第 21 章 JsonPath

概述

JsonPath 语言提供了一种方便的语法,用于提取 JSON 消息的部分。JSON 的语法与 XPath 类似,但它用于从 JSON 消息提取 JSON 对象,而不是对 XML 执行操作。jsonpath DSL 命令可用作表达式或 predicate(空结果解释为布尔值 false)。

添加 JsonPath 软件包

要在 Camel 路由中使用 JsonPath,您需要将对 camel-jsonpath 的依赖添加到项目中,如下所示:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jsonpath</artifactId>
  <version>${camel-version}</version>
</dependency>

Java 示例

以下 Java 示例演示了如何使用 jsonpath() DSL 命令在特定价格范围内选择项目:

from("queue:books.new")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < 10)]")
      .to("jms:queue:book.cheap")
    .when().jsonpath("$.store.book[?(@.price < 30)]")
      .to("jms:queue:book.average")
    .otherwise()
      .to("jms:queue:book.expensive")

如果 JsonPath 查询返回空集,则结果将解释为 false。这样,您可以使用 JsonPath 查询作为 predicate。

XML 示例

以下 XML 示例演示了如何使用 jsonpath DSL 元素在路由中定义 predicates:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <choice>
      <when>
        <jsonpath>$.store.book[?(@.price < 10)]</jsonpath>
        <to uri="mock:cheap"/>
      </when>
      <when>
        <jsonpath>$.store.book[?(@.price < 30)]</jsonpath>
        <to uri="mock:average"/>
      </when>
      <otherwise>
        <to uri="mock:expensive"/>
      </otherwise>
    </choice>
  </route>
</camelContext>

简单的语法

您希望使用 jsonpath 语法定义基本 predicate 时,很难记住语法。例如,要找到所有无线图书,您必须按如下方式编写语法:

$.store.book[?(@.price < 20)]

但是,如果您可以按以下方式写入它:

store.book.price < 20

如果只想使用价格键查看节点,您也可以省略该路径:

price < 20

为了支持此操作,有一个 EasyPredicateParser,您可以使用基本样式来定义 predicate。这意味着 predicate 不能使用 $ 符号启动,且只能包含一个 operator。简单语法如下:

left OP right

您可以在正确的 Operator 中使用 Camel 简单语言,例如:

store.book.price < ${header.limit}

支持的消息正文类型

Camel JSonPath 支持使用以下类型的消息正文:

类型描述

File

从文件读取

字符串

普通字符串

map

将正文作为 java.util.Map 类型

list

java.util.List 类型的消息正文

POJO

可选的 Jackson 位于类路径上,那么 camel-jsonpath 可以使用 Jackson 将消息正文读为 POJO,并转换为 java.util.Map,它由 JSonPath 支持。例如,您可以将 camel-jackson 作为依赖项添加,以包含 Jackson。

InputStream

如果上述类型都不匹配,则 Camel 会尝试将消息正文读为 java.io.InputStream

如果消息正文不受支持,则默认抛出异常,但您可以将 JSonPath 配置为禁止异常。

隐藏异常

如果没有找到 jsonpath 表达式配置的路径,JSONPath 将抛出异常。通过将 SuppressExceptions 选项设置为 true 来忽略异常。例如,在下面的代码中,将 true 选项添加为 jsonpath 参数的一部分:

from("direct:start")
    .choice()
        // use true to suppress exceptions
        .when().jsonpath("person.middlename", true)
            .to("mock:middle")
        .otherwise()
            .to("mock:other");

在 XML DSL 中,使用以下语法:

<route>
  <from uri="direct:start"/>
  <choice>
    <when>
      <jsonpath suppressExceptions="true">person.middlename</jsonpath>
      <to uri="mock:middle"/>
    </when>
    <otherwise>
      <to uri="mock:other"/>
    </otherwise>
  </choice>
</route>

JsonPath injection

在使用 bean 集成来调用 bean 方法时,您可以使用 JsonPath 从消息中提取值,并将它绑定到 method 参数。例如:

// Java
public class Foo {

    @Consume(uri = "activemq:queue:books.new")
    public void doSomething(@JsonPath("$.store.book[*].author") String author, @Body String json) {
      // process the inbound message here
    }
}

内联简单表达式

Camel 2.18 中的新功能.

Camel 在 JsonPath 表达式中支持内联 简单 表达式。简单 语法必须用简单语法表达 简单 语言,如下所示:

from("direct:start")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < `${header.cheap}`)]")
      .to("mock:cheap")
    .when().jsonpath("$.store.book[?(@.price < `${header.average}`)]")
      .to("mock:average")
    .otherwise()
      .to("mock:expensive");

通过设置 选项 allow Simple =false (如下所示)关闭对 Simple 表达式的支持。

Java:

// Java DSL
.when().jsonpath("$.store.book[?(@.price < 10)]", false, false)

XML DSL:

// XML DSL
<jsonpath allowSimple="false">$.store.book[?(@.price &lt; 10)]</jsonpath>

参考

有关 JsonPath 的详情,请查看 JSonPath 项目页面