添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

JSONPath

Since Camel 2.13

Camel supports JSONPath to allow using Expression or Predicate on JSON messages.

Examples

For example, you can use JSONPath in a Predicate with the Content-Based Router EIP.

  • Java

  • XML 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");
<route>
  <from uri="direct:start"/>
  <choice>
      <jsonpath>$.store.book[?(@.price &lt; 10)]</jsonpath>
      <to uri="mock:cheap"/>
    </when>
      <jsonpath>$.store.book[?(@.price &lt; 30)]</jsonpath>
      <to uri="mock:average"/>
    </when>
    <otherwise>
      <to uri="mock:expensive"/>
    </otherwise>
  </choice>
</route>

Inline Simple expressions

It’s possible to inlined Simple language in the JSONPath expression using the simple syntax ${xxx} .

An example is shown below:

  • Java

  • XML DSL

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");
<route>
  <from uri="direct:start"/>
  <choice>
      <jsonpath>$.store.book[?(@.price &lt; ${header.cheap})]</jsonpath>
      <to uri="mock:cheap"/>
    </when>
      <jsonpath>$.store.book[?(@.price &lt; ${header.average})]</jsonpath>
      <to uri="mock:average"/>
    </when>
    <otherwise>
      <to uri="mock:expensive"/>
    </otherwise>
  </choice>
</route>

You can turn off support for inlined Simple expression by setting the option allowSimple to false as shown:

  • Java

  • XML DSL

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

JSONPath injection

You can use Bean Integration to invoke a method on a bean and use various languages such as JSONPath (via the @JsonPath annotation) to extract a value from the message and bind it to a method parameter, as shown below:

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