Added camel-jsonpath language to Gitbook
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/802c3dbb Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/802c3dbb Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/802c3dbb Branch: refs/heads/master Commit: 802c3dbb55943d2ed616b5e82f6f30bb0f35932b Parents: a9e450b Author: Andrea Cosentino <anco...@gmail.com> Authored: Mon May 2 08:47:56 2016 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Mon May 2 08:47:56 2016 +0200 ---------------------------------------------------------------------- .../camel-jsonpath/src/main/docs/jsonpath.adoc | 156 +++++++++++++++++++ docs/user-manual/en/SUMMARY.md | 1 + 2 files changed, 157 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/802c3dbb/components/camel-jsonpath/src/main/docs/jsonpath.adoc ---------------------------------------------------------------------- diff --git a/components/camel-jsonpath/src/main/docs/jsonpath.adoc b/components/camel-jsonpath/src/main/docs/jsonpath.adoc new file mode 100644 index 0000000..cf1eeee --- /dev/null +++ b/components/camel-jsonpath/src/main/docs/jsonpath.adoc @@ -0,0 +1,156 @@ +[[JSonPath-JSonPath]] +JSonPath +~~~~~~~~ + +*Available as of Camel 2.13* + +Camel supports https://code.google.com/p/json-path/[JSonPath] to allow +using link:expression.html[Expression] or link:predicate.html[Predicate] +on json messages. + +[source,java] +----------------------------------------------------- +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-UsingXMLconfiguration]] +Using XML configuration +^^^^^^^^^^^^^^^^^^^^^^^ + +If you prefer to configure your routes in your link:spring.html[Spring] +XML file then you can use link:jsonpath.html[JSonPath] expressions as +follows + +[source,xml] +------------------------------------------------------------------------- + <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-Syntax]] +Syntax +^^^^^^ + +See the https://code.google.com/p/json-path/[JSonPath] project page for +further examples. + +[[JSonPath-Suppressexceptions]] +Suppress exceptions +~~~~~~~~~~~~~~~~~~~ + +*Available as of Camel 2.16* + +By default jsonpath will throw an exception if the json payload does not +have a valid path accordingly to the configured jsonpath expression. In +some use-cases you may want to ignore this in case the json payload +contains optional data. Therefore you can set the option +suppressExceptions to true to ignore this as shown: + +[source,java] +--------------------------------------------------- +from("direct:start") + .choice() + // use true to suppress exceptions + .when().jsonpath("person.middlename", true) + .to("mock:middle") + .otherwise() + .to("mock:other"); +--------------------------------------------------- + +And in XML DSL: + +[source,xml] +-------------------------------------------------------------------------- + <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> +-------------------------------------------------------------------------- + + + +This option is also available on the `@JsonPath` annotation. + +[[JSonPath-JSonPathinjection]] +JSonPath injection +~~~~~~~~~~~~~~~~~~ + +You can use link:bean-integration.html[Bean Integration] to invoke a +method on a bean and use various languages such as JSonPath to extract a +value from the message and bind it to a method parameter. + +For example + +[source,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 + } +} +--------------------------------------------------------------------------------------------------- + +[[JSonPath-EncodingDetection]] +Encoding Detection +~~~~~~~~~~~~~~~~~~ + +*Since Camel version 2.16*, the encoding of the JSON document is +detected automatically, if the document is encoded in unicode  (UTF-8, +UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE ) as specified in  RFC-4627. If +the encoding is a non-unicode encoding, you can either make sure that +you enter the document in String format to the JSONPath component or you +can specify the encoding in the header "*CamelJsonPathJsonEncoding*" +(JsonpathConstants.HEADER_JSON_ENCODING). + +[[JSonPath-Dependencies]] +Dependencies +^^^^^^^^^^^^ + +To use JSonPath in your camel routes you need to add the a dependency on +*camel-jsonpath* which implements the JSonPath language. + +If you use maven you could just add the following to your pom.xml, +substituting the version number for the latest & greatest release (see +link:download.html[the download page for the latest versions]). + +[source,xml] +----------------------------------------- +<dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jsonpath</artifactId> + <version>x.x.x</version> +</dependency> +----------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/802c3dbb/docs/user-manual/en/SUMMARY.md ---------------------------------------------------------------------- diff --git a/docs/user-manual/en/SUMMARY.md b/docs/user-manual/en/SUMMARY.md index e0e9eab..e471da1 100644 --- a/docs/user-manual/en/SUMMARY.md +++ b/docs/user-manual/en/SUMMARY.md @@ -194,6 +194,7 @@ * [Expession Languages](languages.adoc) * [Bean Language](bean-language.adoc) * [Constant](constant.adoc) + * [Jsonpath](jsonpath.adoc) * [SQL](josql.adoc) * Data Formats