Author: buildbot
Date: Wed May 24 13:19:16 2017
New Revision: 1012822
Log:
Production update by buildbot for camel
Modified:
websites/production/camel/content/cache/main.pageCache
websites/production/camel/content/rest-dsl.html
websites/production/camel/content/rest.html
Modified: websites/production/camel/content/cache/main.pageCache
==============================================================================
Binary files - no diff available.
Modified: websites/production/camel/content/rest-dsl.html
==============================================================================
--- websites/production/camel/content/rest-dsl.html (original)
+++ websites/production/camel/content/rest-dsl.html Wed May 24 13:19:16 2017
@@ -85,7 +85,7 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2 id="RestDSL-RestDSL">Rest
DSL</h2><p><strong>Available as of Camel 2.14</strong></p><p>Apache Camel
offers a REST styled DSL which can be used with Java or XML. The intention is
to allow end users to define REST services using a REST style with verbs such
as get, post, delete etc.</p><h4 id="RestDSL-Howitworks">How it
works</h4><p>The Rest DSL is a facade that builds <a shape="rect"
href="rest.html">Rest</a> endpoints as consumers for Camel routes. The
actual REST transport is leveraged by using Camel REST components such
as <a shape="rect" href="restlet.html">Restlet</a>, <a shape="rect"
href="spark-rest.html">Spark-rest</a>, and others that has native REST
integration.</p><h3 id="RestDSL-ComponentssupportingRestDSL">Components
supporting Rest DSL</h3><p>The following Camel components supports the Rest
DSL. See the bottom of this page for how to integrate a component with the Rest
DSL.</p><ul><li>camel-coap</li><li><a shape="
rect" href="netty-http.html">camel-netty-http</a> (also supports <a
shape="rect" href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="netty4-http.html">camel-netty4-http</a> (also supports <a
shape="rect" href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="jetty.html">camel-jetty</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="restlet.html">camel-restlet</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="servlet.html">camel-servlet</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="spark-rest.html">camel-spark-rest</a> (also supports <a
shape="rect" href="swagger-java.html">Swagger Java</a> from Camel 2.17
onwards)</li><li><a shape="rect"
href="undertow.html">camel-undertow</a> (also supports <a sh
ape="rect" href="swagger-java.html">Swagger Java</a> from Camel 2.17
onwards)</li></ul><h3 id="RestDSL-RestDSLwithJava">Rest DSL with Java</h3><p>To
use the Rest DSL in Java then just do as with regular Camel routes by extending
the <code>RouteBuilder</code> and define the routes in
the <code>configure</code> method.</p><p>A simple REST service can be
define as follows, where we use rest() to define the services as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+<div class="wiki-content maincontent"><h2 id="RestDSL-RestDSL">Rest
DSL</h2><p><strong>Available as of Camel 2.14</strong></p><p>Apache Camel
offers a REST styled DSL which can be used with Java or XML. The intention is
to allow end users to define REST services using a REST style with verbs such
as <strong><code>GET</code>,
<code>POST</code></strong>, <strong><code>DELETE</code></strong>
etc.</p><h4 id="RestDSL-Howitworks">How it works</h4><p>The Rest DSL is a
facade that builds <a shape="rect" href="rest.html">Rest</a> endpoints as
consumers for Camel routes. The actual REST transport is leveraged by using
Camel REST components such as <a shape="rect"
href="restlet.html">Restlet</a>, <a shape="rect"
href="spark-rest.html">Spark-rest</a>, and others that has native REST
integration.</p><h3 id="RestDSL-ComponentssupportingRestDSL">Components
supporting Rest DSL</h3><p>The following Camel components supports the Rest
DSL. See the bottom of this page for how to int
egrate a component with the Rest DSL.</p><ul><li>camel-coap</li><li><a
shape="rect" href="netty-http.html">camel-netty-http</a> (also
supports <a shape="rect" href="swagger-java.html">Swagger
Java</a>)</li><li><a shape="rect"
href="netty4-http.html">camel-netty4-http</a> (also supports <a
shape="rect" href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="jetty.html">camel-jetty</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="restlet.html">camel-restlet</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="servlet.html">camel-servlet</a> (also supports <a shape="rect"
href="swagger-java.html">Swagger Java</a>)</li><li><a shape="rect"
href="spark-rest.html">camel-spark-rest</a> (also supports <a
shape="rect" href="swagger-java.html">Swagger Java</a> from <strong>Camel
2.17</strong>)</li><li
><a shape="rect" href="undertow.html">camel-undertow</a> (also
>supports <a shape="rect" href="swagger-java.html">Swagger
>Java</a> from <strong>Camel 2.17</strong>)</li></ul><h3
>id="RestDSL-RestDSLwithJava">Rest DSL with Java</h3><p>To use the Rest DSL in
>Java then just do as with regular Camel routes by extending
>the <strong><code>RouteBuilder</code></strong> and define the routes in
>the <strong><code>configure()</code></strong> method.</p><p>A simple
>REST service can be define as follows, where we
>use <strong><code>rest()</code></strong> to define the services as shown
>below:</p><div class="code panel pdl" style="border-width: 1px;"><div
>class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ protected RouteBuilder
createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
@@ -102,8 +102,8 @@
}
};
}]]></script>
-</div></div><p> </p><p>This defines a REST service with the following url
mappings:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Base Path</th><th colspan="1" rowspan="1"
class="confluenceTh">Uri template</th><th colspan="1" rowspan="1"
class="confluenceTh">Verb</th><th colspan="1" rowspan="1"
class="confluenceTh">Consumes</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><span>/say</span></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>/hello</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>get</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><em>all</em></p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p>/say</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>/bye</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>get</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>application/json</p></td></tr><tr><td colspa
n="1" rowspan="1" class="confluenceTd"><p>/say</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>/bye</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>post</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><em>all</em></p></td></tr></tbody></table></div><p>Notice
that in the REST service we route directly to a Camel endpoint using the to().
This is because the Rest DSL has a short-hand for routing directly to an
endpoint using to(). An alternative is to embed a Camel route directly using
route() - there is such an example further below.</p><h3
id="RestDSL-RestDSLwithXML">Rest DSL with XML</h3><p>The REST DSL supports the
XML DSL also using either Spring or Blueprint. The example above can be define
in XML as shown below:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <camelContext
xmlns="http://camel.apache.org/schema/spring">
+</div></div><p> </p><p>This defines a REST service with the following URL
mappings:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Base Path</th><th colspan="1" rowspan="1"
class="confluenceTh">URI Template</th><th colspan="1" rowspan="1"
class="confluenceTh">Verb</th><th colspan="1" rowspan="1"
class="confluenceTh">Consumes</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><span><code>/say</code><br
clear="none"></span></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>/hello</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>GET</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><em>all</em></p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>/say</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>/bye</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>GET</code></p></td><
td colspan="1" rowspan="1"
class="confluenceTd"><p><code>application/json</code></p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>/say</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>/bye</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>POST</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><em>all</em></p></td></tr></tbody></table></div><p>Notice
that in the REST service we route directly to a Camel endpoint using the
<strong><code>to()</code></strong>. This is because the Rest DSL has a
short-hand for routing directly to an endpoint using
<strong><code>to()</code></strong>. An alternative is to embed a Camel route
directly using <strong><code>route()</code></strong> - there is such an
example further below.</p><h3 id="RestDSL-RestDSLwithXML">Rest DSL with
XML</h3><p>The REST DSL supports the XML DSL also using either Spring or
Blueprint. The example above can be define in XML as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <camelContext
xmlns="http://camel.apache.org/schema/spring">
<rest path="/say">
<get uri="/hello">
<to uri="direct:hello"/>
@@ -128,7 +128,7 @@
</transform>
</route>
</camelContext>]]></script>
-</div></div><p> </p><h3 id="RestDSL-Usingbasepath">Using base
path</h3><p>The REST DSL allows to define base path to make the DSL a bit more
DRY. For example to define a customer path, we can set the base path in
rest("/customer") and then provide the uri templates in the verbs, as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+</div></div><p> </p><h3 id="RestDSL-UsingBasePath">Using Base
Path</h3><p>The REST DSL allows to define base path to make the DSL a bit more
DRY. For example to define a customer path, we can set the base path
in <strong><code>rest("/customer")</code></strong> and then provide the
URI templates in the verbs, as shown below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ rest("/customers/")
.get("/{id}").to("direct:customerDetail")
.get("/{id}/orders").to("direct:customerOrders")
@@ -136,7 +136,7 @@
]]></script>
</div></div><p> </p><p>And using XML DSL it becomes:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <rest path="/customers/">
+<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <rest path="/customers/">
<get uri="/{id}">
<to uri="direct:customerDetail"/>
</get>
@@ -147,8 +147,8 @@
<to uri="direct:customerNewOrder"/>
</post>
</rest>]]></script>
-</div></div><div class="confluence-information-macro
confluence-information-macro-tip"><span class="aui-icon aui-icon-small
aui-iconfont-approve confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>The REST DSL will take care of
duplicate path separators when using base path and uri templates. In the
example above the rest base path ends with a slash ( / ) and the verb starts
with a slash ( / ). But Apache Camel will take care of this and remove the
duplicated slash.</p></div></div><p>It is not required to use both base path
and uri templates. You can omit the bast path and define the base path and uri
template in the verbs only. The example above can be defined as:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <rest>
+</div></div><div class="confluence-information-macro
confluence-information-macro-tip"><span class="aui-icon aui-icon-small
aui-iconfont-approve confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>The REST DSL will take care of
duplicate path separators when using base path and URI templates. In the
example above the rest base path ends with a slash
( <strong><code>/</code></strong> ) and the verb starts with a slash
( <strong><code>/</code></strong> ). But Apache Camel will take care of
this and remove the duplicated slash.</p></div></div><p>It is not required to
use both base path and URI templates. You can omit the bast path and define the
base path and URI template in the verbs only. The example above can be defined
as:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <rest>
<get uri="/customers/{id}">
<to uri="direct:customerDetail"/>
</get>
@@ -159,8 +159,8 @@
<to uri="direct:customerNewOrder"/>
</post>
</rest>]]></script>
-</div></div><h3 id="RestDSL-UsingDynamicTo">Using Dynamic
To</h3><p><strong>Available as of Camel 2.16</strong></p><p>The <a
shape="rect" href="rest-dsl.html">Rest DSL</a> supports the new .toD
<toD> as dynamic to in the rest-dsl. For example to do a request/reply
over <a shape="rect" href="jms.html">JMS</a> where the queue name is
dynamic defined</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ public void configure() throws Exception {
+</div></div><h3 id="RestDSL-UsingDynamicto()">Using
Dynamic <code>to()</code></h3><p><strong>Available as of Camel
2.16</strong></p><p>The <a shape="rect" href="rest-dsl.html">Rest DSL</a>
supports the new <strong><code>.toD</code></strong>
or <strong><code><toD></code></strong> as dynamic to in the
<strong><code>rest-dsl</code></strong>. For example to do a request/reply
over <a shape="rect" href="jms.html">JMS</a> where the queue name is
dynamic defined:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ public void configure() throws Exception {
rest("/say")
.get("/hello/{language}").toD("jms:queue:hello-${header.language}");
}]]></script>
@@ -170,7 +170,7 @@
<toD uri="jms:queue:hello-${header.language}"/>
</get>
<rest>]]></script>
-</div></div><p> </p><p>See more details at <a shape="rect"
href="message-endpoint.html">Message Endpoint</a> about the dynamic to, and
what syntax it supports. By default it uses the <a shape="rect"
href="simple.html">Simple</a> language, but it has more power than so.</p><h3
id="RestDSL-EmbeddingCamelroutes">Embedding Camel routes</h3><p>Each of the
rest service becomes a Camel route, so in the first example we have 2 x
get and 1 x post REST service, which each become a Camel route. And we have 2
regular Camel routes, meaning we have 3 + 2 = 5 routes in
total. </p><p>There are two route modes with the Rest DSL</p><ul><li><span
style="line-height: 1.4285715;">mini using a singular to</span></li><li><span
style="line-height: 1.4285715;">embedding a Camel route using
route </span></li></ul><p><span style="line-height: 1.4285715;">The first
example is using the former with a singular to. And that is why we end up with
3 + 2 = 5 total routes.</span></p><p><
span style="line-height: 1.4285715;">The same example could use embedded Camel
routes, which is shown below:</span></p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p> </p><p>See more details at <a shape="rect"
href="message-endpoint.html">Message Endpoint</a> about the dynamic to, and
what syntax it supports. By default it uses the <a shape="rect"
href="simple.html">Simple</a> language, but it has more power than so.</p><h3
id="RestDSL-EmbeddingCamelRoutes">Embedding Camel Routes</h3><p>Each of the
rest service becomes a Camel route, so in the first example we
have <strong><code>2 x GET</code></strong> and <strong><code>1 x
POST</code></strong> REST service, which each become a Camel route. We also
have two regular Camel routes. Therefore we have <span><strong><code>3 + 2
= 5</code></strong></span> routes in total. </p><p>There are two route
modes with the Rest DSL:</p><ul><li><span style="line-height: 1.4285715;">mini
using a singular to</span></li><li><span style="line-height:
1.4285715;">embedding a Camel route using route </span></li></ul><p><span
style="line-height: 1.4285715;">The
first example is using the former with a singular
<strong><code>to()</code></strong>. That's why we end up
with <strong><code>3 + 2 = 5</code></strong> total
routes.</span></p><p><span style="line-height: 1.4285715;">The same example
could use embedded Camel routes:</span></p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ protected RouteBuilder
createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
@@ -182,11 +182,11 @@
.post().to("mock:update");
};
}]]></script>
-</div></div><p><span style="line-height: 1.4285715;">In the example above, we
are embedding routes directly in the rest service using .route(). Notice we
need to use .endRest() to tell Camel where the route ends, so we
can <em>go back</em> to the Rest DSL and continue defining REST
services.</span></p><div class="confluence-information-macro
confluence-information-macro-tip"><p class="title">Configuring route
options</p><span class="aui-icon aui-icon-small aui-iconfont-approve
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>In the embedded route you can
configure the route settings such as routeId, autoStartup and various other
options you can set on routes
today.</p><pre>.get().route().routeId("myRestRoute").autoStartup(false).transform().constant("Hello
World");</pre></div></div><h3 id="RestDSL-ManagingRestservices"><span
style="font-size: 16.0px;line-height: 1.5625;">Managing Rest
services</span></h3><p>Each of the rest service bec
omes a Camel route, so in the first example we have 2 x get and 1 x post REST
service, which each become a Camel route. This makes it <em>the same</em>
from Camel to manage and run these services - as they are just Camel routes.
This means any tooling and API today that deals with Camel routes, also work
with the REST services.</p><p>This means you can use JMX to stop/start routes,
and also get the JMX metrics about the routes, such as number of message
processed, and their performance statistics.</p><p>There is also a Rest
Registry JMX MBean that contains a registry of all REST services which has been
defined. </p><h3 id="RestDSL-BindingtoPOJOsusing">Binding to POJOs
using</h3><p>The Rest DSL supports automatic binding json/xml contents to/from
POJOs using Camels <a shape="rect" href="data-format.html">Data
Format</a>. By default the binding mode is off, meaning there is no automatic
binding happening for incoming and outgoing messages.</p><p>You may want to use
bind
ing if you develop POJOs that maps to your REST services request and response
types. This allows you as a developer to work with the POJOs in Java
code.</p><p>The binding modes are:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Binding Mode</th><th colspan="1" rowspan="1"
class="confluenceTh">Description</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>off</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Binding is turned off. This is the default
option.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>auto</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Binding is enabled and Camel is relaxed and support
json, xml or both if the needed data formats are included in the classpath.
Notice that if for example <code>camel-jaxb</code> is not on the classpath,
then XML binding is not enabled.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p
>json</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>Binding
>to/from json is enabled, and requires a json capabile data format on the
>classpath. By default Camel will use <code>json-jackson</code> as the data
>format. See the INFO box below for more details.</p></td></tr><tr><td
>colspan="1" rowspan="1" class="confluenceTd"><p>xml</p></td><td colspan="1"
>rowspan="1" class="confluenceTd"><p>Binding to/from xml is enabled, and
>requires <code>camel-jaxb</code> on the classpath. <span>See the INFO box
>below for more details.</span></p></td></tr><tr><td colspan="1" rowspan="1"
>class="confluenceTd"><p>json_xml</p></td><td colspan="1" rowspan="1"
>class="confluenceTd"><p>Biding to/from json and xml is enabled and requires
>both data formats to be on the classpath. <span>See the INFO box below for
>more details.</span></p></td></tr></tbody></table></div><div
>class="confluence-information-macro confluence-information-macro-tip"><span
>class="aui-icon aui-icon-small aui-iconfont-approv
e confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>From <strong>Camel 2.14.1</strong>
onwards when using camel-jaxb for xml bindings, then you can use the option
<code>mustBeJAXBElement</code> to relax the output message body must be a class
with JAXB annotations. You can use this in situations where the message body is
already in XML format, and you want to use the message body as-is as the output
type. If that is the case, then set the <span>dataFormatProperty option
<code>mustBeJAXBElement</code><span> to <code>false</code>
value.</span></span></p></div></div><div class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>From <strong>Camel
2.16.3</strong> onwards the binding from POJO to JSon/JAXB will only
happen if the <code>content-type</code> header
includes the
word <code>json</code> or <code>xml</code> representatively.
This allows you to specify a custom content-type if the message body should
not attempt to be marshalled using the binding. For example if the message body
is a custom binary payload etc.</p></div></div><p><span style="line-height:
1.42857;">To use binding you must include the necessary data formats on the
classpath, such as </span><code style="line-height:
1.42857;">camel-jaxb</code><span style="line-height: 1.42857;">
and/or </span><code style="line-height:
1.42857;">camel-jackson</code><span style="line-height: 1.42857;">. And then
enable the binding mode. You can configure the binding mode globally on the
rest configuration, and then override per rest service as well.</span></p><p>To
enable binding you configure this in Java DSL as shown below</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
+</div></div><p><span style="line-height: 1.4285715;">In the example above, we
are embedding routes directly in the rest service using
<strong><code>.route()</code></strong>. </span></p><div
class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Note</strong>: we need to
use <strong><code>.endRest()</code></strong> to tell Camel where the route
ends, so we can <em>go back</em> to the Rest DSL and continue defining
REST services.</div></div><div class="confluence-information-macro
confluence-information-macro-tip"><p class="title">Configuring route
options</p><span class="aui-icon aui-icon-small aui-iconfont-approve
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>In the embedded route you can
configure the route settings such as <strong><code>routeId</code></s
trong>, <strong><code>autoStartup</code></strong> and various other
options you can set on routes
today.</p><pre>.get().route().routeId("myRestRoute").autoStartup(false).transform().constant("Hello
World");</pre></div></div><h3 id="RestDSL-ManagingRestServices"><span
style="font-size: 16.0px;line-height: 1.5625;">Managing Rest
Services</span></h3><p>Each of the rest service becomes a Camel route, so in
the first example we have <strong><code>2 x GET</code></strong>
and <strong><code>1 x POST</code></strong> REST service, which each become
a Camel route. This makes it <em>the same</em> from Camel to manage and
run these services - as they are just Camel routes. This means any tooling and
API today that deals with Camel routes, also work with the REST
services.</p><p>This means you can use JMX to stop/start routes, and also get
the JMX metrics about the routes, such as number of message processed, and
their performance statistics.</p><p>There is also a Rest Registr
y JMX MBean that contains a registry of all REST services which has been
defined. </p><h3 id="RestDSL-BindingtoPOJOsUsing">Binding to POJOs
Using</h3><p>The Rest DSL supports automatic
binding <strong><code>json/xml</code></strong> contents to/from POJOs
using Camels <a shape="rect" href="data-format.html">Data Format</a>. By
default the binding mode is off, meaning there is no automatic binding
happening for incoming and outgoing messages.</p><p>You may want to use binding
if you develop POJOs that maps to your REST services request and response
types. This allows you as a developer to work with the POJOs in Java
code.</p><p>The binding modes are:</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Binding Mode</th><th colspan="1" rowspan="1"
class="confluenceTh">Description</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>off</code></p></td><td colspan="1" rowspan="1"
class="conf
luenceTd"><p>Binding is turned off. This is the default
option.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>auto</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Binding is enabled and Camel is relaxed and support
JSON, XML or both if the needed data formats are included in the
classpath.</p><p><strong>Note</strong>: if, for example,
<strong><code>camel-jaxb</code></strong> is not on the classpath, then XML
binding is not enabled.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>json</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Binding to/from JSON is enabled, and requires a
json capabile data format on the classpath. By default Camel will use
<strong><code>json-jackson</code></strong> as the data format.</p><p>See the
INFO box below for more details.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>xml</code></p></td><td colspan="1" rowspan="1"
class=
"confluenceTd"><p>Binding to/from XML is enabled, and requires
<strong><code>camel-jaxb</code></strong> on the classpath.
<span> </span></p><p><span>See the INFO box below for more
details.</span></p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>json_xml</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Biding to/from JSON and XML is
enabled and requires both data formats to be on the classpath.
<span> </span></p><p><span>See the INFO box below for more
details.</span></p></td></tr></tbody></table></div><div
class="confluence-information-macro confluence-information-macro-tip"><span
class="aui-icon aui-icon-small aui-iconfont-approve
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>From <strong>Camel
2.14.1</strong>: when using <strong><code>camel-jaxb</code></strong> for
XML bindings, then you can use the option
<strong><code>mustBeJAXBElement</code></strong> to relax the
output message body must be a class with JAXB annotations. You can use this in
situations where the message body is already in XML format, and you want to use
the message body as-is as the output type. If that is the case, then set the
<span><strong><code>dataFormatProperty</code></strong> option
<strong><code>mustBeJAXBElement=false</code></strong><span>.</span></span></p></div></div><div
class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><p>From <strong>Camel
2.16.3</strong>: the binding from POJO to JSON/JAXB will only happen if
the <strong><code>content-type</code></strong> header includes the
word <strong><code>json</code></strong> or <strong><code>xml</code></strong>
respectively. This allows you to specify a
custom <strong><code>content-type</code></strong> if the message b
ody should not attempt to be marshaled using the binding. For example if the
message body is a custom binary payload etc.</p></div></div><p><span
style="line-height: 1.42857;">To use binding you must include the necessary
data formats on the classpath, such as </span><strong><code
style="line-height: 1.42857;">camel-jaxb</code></strong><span
style="line-height: 1.42857;"> and/or </span><strong><code
style="line-height: 1.42857;">camel-jackson</code></strong><span
style="line-height: 1.42857;">. And then enable the binding mode. You can
configure the binding mode globally on the rest configuration, and then
override per rest service as well.</span></p><p>To enable binding you configure
this in Java DSL as shown below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);]]></script>
-</div></div><p>And in XML DSL</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <restConfiguration
bindingMode="auto" component="restlet"
port="8080"/>]]></script>
-</div></div><p> </p><p>When binding is enabled Camel will bind the
incoming and outgoing messages automatic, accordingly to the content type of
the message. If the message is json, then json binding happens; and so if the
message is xml then xml binding happens. The binding happens for incoming and
reply messages. The table below summaries what binding occurs for incoming and
reply messages. </p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Message Body</th><th colspan="1" rowspan="1"
class="confluenceTh">Direction</th><th colspan="1" rowspan="1"
class="confluenceTh">Binding Mode</th><th colspan="1" rowspan="1"
class="confluenceTh">Message Body</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>XML</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Incoming</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>auto<br clear="none">xml<br
clear="none">json_xml </p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Outgoing</p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>auto</p><p>xml</p><p>json_xml </p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>XML</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>JSON</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Incoming</p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>auto</p><p>json</p><p>json_xml </p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Outgoing</p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><p>auto</p><p>json</p><p>json_xml </p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p>JSON</p></td></tr></tbody></table><
/div><p> </p><p>When using binding you must also configure what POJO type
to map to. This is mandatory for incoming messages, and optional for
outgoing. </p><p>For example to map from xml/json to a pojo
class <code>UserPojo</code> you do this in Java DSL as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+</div></div><p>And in XML DSL:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<restConfiguration
bindingMode="auto" component="restlet"
port="8080"/>]]></script>
+</div></div><p> </p><p>When binding is enabled Camel will bind the
incoming and outgoing messages automatic, accordingly to the content type of
the message. If the message is JSON, then JSON binding happens; and so if the
message is XML then XML binding happens. The binding happens for incoming and
reply messages. The table below summaries what binding occurs for incoming and
reply messages. </p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Message Body</th><th colspan="1" rowspan="1"
class="confluenceTh">Direction</th><th colspan="1" rowspan="1"
class="confluenceTh">Binding Mode(s)</th><th colspan="1" rowspan="1"
class="confluenceTh">Message Body</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>XML</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Incoming</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><ul><li><code>auto</code></li><li><code>xml</code></li><li><code>js
on_xml</code> </li></ul></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>POJO</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>POJO</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Outgoing</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><ul><li><code>auto</code></li><li><code>xml</code></li><li><code>json_xml</code> </li></ul></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>XML</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>JSON</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Incoming</p></td><td colspan="1"
rowspan="1"
class="confluenceTd"><ul><li><code>auto</code></li><li><code>json</code></li><li><code>json_xml</code></li></ul></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>POJO</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Outgoing</p></td><td colspan="1"
rowspan="1" class="confluence
Td"><ul><li><code>auto</code></li><li><code>json</code></li><li><code>json_xml</code></li></ul></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p>JSON</p></td></tr></tbody></table></div><p> </p><p>When
using binding you must also configure what POJO type to map to. This is
mandatory for incoming messages but optional for outgoing. </p><p>For
example, to map from <strong><code>xml/json</code></strong> to a POJO
class <strong><code>UserPojo</code></strong> you do this in Java DSL as
shown below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[// configure to use restlet on localhost with
the given port
// and enable auto binding mode
restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);
@@ -195,7 +195,7 @@ restConfiguration().component("rest
rest("/users/")
.post().type(UserPojo.class)
.to("direct:newUser");]]></script>
-</div></div><p>Notice we use <code>type</code> to define the incoming
type. We can optionally define an outgoing type (which can be a good idea, to
make it known from the DSL and also for tooling and JMX APIs to know both the
incoming and outgoing types of the REST services.). To define the outgoing
type, we use <code>outType</code> as shown below:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>Notice we use <code>type</code> to define the incoming
type. We can optionally define an outgoing type (which can be a good idea, to
make it known from the DSL and also for tooling and JMX APIs to know both the
incoming and outgoing types of the REST services.). To define the outgoing type
we use <strong><code>outType</code></strong> as shown below:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[// configure to use restlet on localhost with
the given port
// and enable auto binding mode
restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);
@@ -204,7 +204,7 @@ restConfiguration().component("rest
rest("/users/")
.post().type(UserPojo.class).outType(CountryPojo.class)
.to("direct:newUser");]]></script>
-</div></div><p><span style="line-height: 1.4285715;"><br
clear="none"></span></p><p><span style="line-height:
1.4285715;">The </span><code style="line-height:
1.4285715;">UserPojo</code><span style="line-height: 1.4285715;"> is just a
plain pojo with getter/setter as shown:</span></p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p><span style="line-height: 1.4285715;"><br
clear="none"></span></p><p><span style="line-height:
1.4285715;">The </span><strong><code style="line-height:
1.4285715;">UserPojo</code></strong><span style="line-height: 1.4285715;"> is
just a plain POJO with getter/setter as shown:</span></p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class UserPojo {
private int id;
private String name;
@@ -221,7 +221,7 @@ rest("/users/")
this.name = name;
}
}]]></script>
-</div></div><p>The <code>UserPojo</code> only supports json, as XML
requires to use JAXB annotations, so we can add those annotations if we want to
support XML also</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+</div></div><p>The <strong><code>UserPojo</code></strong> only supports
JSON, as XML requires to use JAXB annotations, so we can add those annotations
if we want to support XML also:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserPojo {
@@ -242,38 +242,40 @@ public class UserPojo {
this.name = name;
}
}
-
]]></script>
-</div></div><p>By having the JAXB annotations the POJO supports both json and
xml bindings.</p><h3 id="RestDSL-ConfiguringRestDSL"><span style="line-height:
1.5625;">Configuring Rest DSL</span></h3><p>The Rest DSL allows to configure
the following options using a builder style</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Option</th><th colspan="1" rowspan="1"
class="confluenceTh">Default</th><th colspan="1" rowspan="1"
class="confluenceTh">Description</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>component</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The Camel Rest component to use for the REST transport,
such as restlet, spark-rest. If no component has been explicit configured, then
Camel will lookup if there is a Camel component that integrates with the Rest
DSL, or if a <code>org.apache.camel.spi.RestConsumerFa
ctory</code> is registered in the registry. If either one is found, then
that is being used.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>scheme</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>http</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The scheme to use for exposing the REST service.
Usually http or https is supported</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>hostname</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The hostname to use for exposing the REST
service.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>port</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The port number to use for exposing the REST service.
Notice if you use servlet component then the port number configured here does
not apply, as the port number in use is
the actual port number the servlet component is using. eg if using Apache
Tomcat its the tomcat http port, if using Apache Karaf its the HTTP service in
Karaf that uses port 8181 by default etc. Though in those situations setting
the port number here, allows tooling and JMX to know the port number, so its
recommended to set the port number to the number that the servlet engine
uses.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>contextPath</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Sets a leading context-path the REST services will be
using. This can be used when using components such as <a shape="rect"
href="servlet.html">SERVLET</a> where the deployed web application is deployed
using a context-path.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>restHostNameResolver</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p> </p></td><td colspan="1" row
span="1" class="confluenceTd"><p>If no hostname has been explicit configured,
then this resolver is used to compute the hostname the REST service will be
using. The resolver supports <code>allLocalIp</code> (<strong>Camel
2.17</strong>), <code>localHostName</code> or <code>localIp</code>. The default
value for Camel 2.16.x or older is localHostName, and allLocalIp from Camel
2.17 onwards.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>bindingMode</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>off</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Whether binding is in use. See further above for more
details.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>skipBindingOnErrorCode</p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>true</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><strong>Camel 2.14.1</strong>: Whether to skip binding
on output if there is a custom HTTP error code header. This allow
s to build custom error messages that do not bind to json / xml etc, as
success messages otherwise will do. See further below for an
example.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>enableCORS</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>false</p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><strong>Camel 2.14.1:</strong> Whether to enable CORS
headers in the HTTP response.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>jsonDataFormat</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Name of specific json data format to use. By default
<code>json-jackson</code> will be used. <strong>Important:</strong> This option
is only for setting a custom name of the data format, not to refer to an
existing data format instance. <strong>Notice:</strong> Currently Jackson is
what we recommend and are using for testing.</p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p>xmlDataFormat</p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Name of specific XML data format to use. By default
<code>jaxb</code> will be used. <strong>Important:</strong><span> This option
is only for setting a custom name of the data format, not to refer to an
existing data format instance. </span><strong>Notice:</strong> Currently only
<code>jaxb</code> is supported.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>componentProperty</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Allows to configure as many additional properties. This
is used to configure component specific options such as for <a
shape="rect" href="restlet.html">Restlet</a> / <a shape="rect"
href="spark-rest.html">Spark-Rest</a> etc. T<span>he options value can use
the # notation to refer to a b
ean to lookup in the </span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>endpointProperty</p></td><td
colspan="1" rowspan="1" class="confluenceTd"> </td><td colspan="1"
rowspan="1" class="confluenceTd"><p><span>Allows to configure as many
additional properties. This is used to configure endpoint specific options for
<span> </span><a shape="rect"
href="restlet.html">Restlet</a><span> / </span><a shape="rect"
href="spark-rest.html">Spark-Rest</a><span> etc. T<span>he options value
can use the # notation to refer to a bean to lookup in the
</span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></span></span></p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>consumerProperty</p></td><td
colspan="1" rowspan="1" class="confluenceTd"> </td><td colspan="1"
rowspan="1" class="confluenceTd"><p><s
pan>Allows to configure as many additional properties. This is used to
configure consumer specific options for </span><span> </span><a
shape="rect" href="restlet.html">Restlet</a><span> / </span><a
shape="rect" href="spark-rest.html">Spark-Rest</a><span> etc. T<span>he
options value can use the # notation to refer to a bean to lookup in the
</span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></span></p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p>dataFormatProperty</p></td><td
colspan="1" rowspan="1" class="confluenceTd"> </td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Allows to configure as many additional
properties. This is used to configure the data format specific options. For
example set property prettyPrint to true to have json outputted in pretty mode.
From <strong>Camel 2.14.1</strong> onwards the keys can be prefixed with
either</p><ul style="list-style-type: square;"><li><p>js
on.in.</p></li><li><p>json.out.</p></li><li><p>xml.in.</p></li><li><p>xml.out.</p></li></ul><p>to
denote that the option is only for either JSON or XML data format, and only
for either the in or the out going. For example a key with value
"xml.out.mustBeJAXBElement" is only for the XML data format for the outgoing. A
key without a prefix is a common key for all situations.</p><p>From
<strong>Camel 2.17</strong> onwards the options value can use the # notation to
refer to a bean to lookup in the <a shape="rect"
href="registry.html">Registry</a></p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>corsHeaderProperty</p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Allows to configure custom CORS
headers.</p></td></tr></tbody></table></div><p> </p><p><span
style="line-height: 1.4285715;">For example to configure to use the spark-rest
component on port 9091, then we can do as follows</span></p><d
iv class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
+</div></div><p>By having the JAXB annotations the POJO supports both JSON and
XML bindings.</p><h3 id="RestDSL-ConfiguringRestDSL"><span style="line-height:
1.5625;">Configuring Rest DSL</span></h3><p>The Rest DSL allows to configure
the following options using a builder style</p><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh">Option</th><th colspan="1" rowspan="1"
class="confluenceTh">Default</th><th colspan="1" rowspan="1"
class="confluenceTh">Description</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>component</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The Camel Rest component to use for the REST transport,
such as <strong><code>restlet</code>,
<code>spark-rest</code></strong>.</p><p>If no component has been explicit
configured, then Camel will lookup if there is a Camel component that
integrates with the Re
st DSL, or if
a <strong><code>org.apache.camel.spi.RestConsumerFactory</code></strong> is
registered in the registry. If either one is found, then that is being
used.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>scheme</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>http</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The scheme to use for exposing the REST service.
Usually <strong><code>http</code></strong>
or <strong><code>https</code></strong> is supported</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>hostname</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The hostname to use for exposing the REST
service.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>port</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspa
n="1" class="confluenceTd"><p>The port number to use for exposing the REST
service.</p><div class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Note</strong>: if you use
servlet component then the port number configured here does not apply, as the
port number in use is the actual port number the servlet component is using,
e.g., if using Apache Tomcat its the tomcat HTTP port, if using Apache Karaf
it's the HTTP service in Karaf that uses port 8181 by default etc. Though in
those situations setting the port number here, allows tooling and JMX to know
the port number, so its recommended to set the port number to the number that
the servlet engine uses.</div></div></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>contextPath</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd">
 </td><td colspan="1" rowspan="1" class="confluenceTd"><p>Sets a leading
context-path the REST services will be using. This can be used when using
components such as <a shape="rect" href="servlet.html">SERVLET</a> where the
deployed web application is deployed using a context-path.</p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>restHostNameResolver</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p> </p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>If no hostname has been explicit
configured, then this resolver is used to compute the hostname the REST service
will be using.</p><p>The resolver
supports:</p><ul><li><strong><code>allLocalIp</code></strong> (<strong>from
Camel
2.17</strong>)</li><li><strong><code>localHostName</code></strong></li><li><strong><code>localIp</code></strong></li></ul><p>For
<strong>Camel 2.16.x</strong> or older:
<strong>localHostName</strong></p><p>From <strong>Camel
2.17</strong>: <stro
ng><code>allLocalIp</code></strong></p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>bindingMode</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>off</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Whether binding is in use. See
further above for more details.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>skipBindingOnErrorCode</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><strong>Camel 2.14.1</strong>:
Whether to skip binding on output if there is a custom HTTP error code
header.</p><p>This allows to build custom error messages that do not bind to
JSON/XML etc, as success messages otherwise will do.</p><p>See below for an
example.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>enableCORS</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>f
alse</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><strong>Camel 2.14.1:</strong> Whether to enable CORS
headers in the HTTP response.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>jsonDataFormat</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Name of specific JSON data format to use. By default
<strong><code>json-jackson</code></strong> will be used.</p><div
class="confluence-information-macro confluence-information-macro-warning"><span
class="aui-icon aui-icon-small aui-iconfont-error
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Important:</strong> This
option is only for setting a custom name of the data format, not to refer to an
existing data format instance.</div></div><div
class="confluence-information-macro
confluence-information-macro-information"><span class="aui-icon aui-icon-small
aui-iconfont
-info confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Note:</strong> Currently
Jackson is what we recommend and are using for
testing.</div></div></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>xmlDataFormat</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Name of specific XML data format to use. By default
<strong><code>jaxb</code></strong> will be used.</p><div
class="confluence-information-macro confluence-information-macro-warning"><span
class="aui-icon aui-icon-small aui-iconfont-error
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Important:</strong><span>
This option is only for setting a custom name of the data format, not to refer
to an existing data format instance. </span></div></div><div
class="confluence-information-macro
confluence-information-macro-information"><span cla
ss="aui-icon aui-icon-small aui-iconfont-info
confluence-information-macro-icon"></span><div
class="confluence-information-macro-body"><strong>Note:</strong> Currently only
<strong><code>jaxb</code></strong> is supported.</div></div></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>componentProperty</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Allows to configure as many additional properties. This
is used to configure component specific options such as for <a
shape="rect" href="restlet.html">Restlet</a> / <a shape="rect"
href="spark-rest.html">Spark-Rest</a> etc. T<span>he options value can use
the <strong><code>#</code></strong> notation to refer to a bean to lookup
in the </span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></p></td></tr><tr><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>endpointProper
ty</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p><span>Allows to configure as many additional
properties. This is used to configure endpoint specific options for
<span> </span><a shape="rect"
href="restlet.html">Restlet</a><span> / </span><a shape="rect"
href="spark-rest.html">Spark-Rest</a><span> etc. T<span>he options value
can use the <span><strong><code>#</code></strong></span> notation to refer
to a bean to lookup in the </span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></span></span></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumerProperty</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p><span>Allows to configure as many additional
properties. This is used to configure consumer specific options for
</span><span> <
/span><a shape="rect"
href="restlet.html">Restlet</a><span> / </span><a shape="rect"
href="spark-rest.html">Spark-Rest</a><span> etc. T<span>he options value
can use the <span><strong><code>#</code></strong></span> notation to refer
to a bean to lookup in the </span><span> </span><a shape="rect"
href="registry.html">Registry</a><span> </span></span></p></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>dataFormatProperty</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Allows to configure as many additional properties. This
is used to configure the data format specific options.</p><p>For example set
property <strong><code>prettyPrint=true</code></strong> to have JSON
outputted in pretty mode.</p><p>From <strong>Camel 2.14.1:</strong> the keys
can be prefixed with either:</p><ul style="list-style-type:
square;"><li><p><code>json.in.</code></p></li><li>
<p><code>json.out.</code></p></li><li><p><code>xml.in.</code></p></li><li><p><code>xml.out.</code></p></li></ul><p>to
denote that the option is only for either JSON or XML data format, and only
for either the in or the out going. For example a key with
value <strong><code>xml.out.mustBeJAXBElement</code></strong> is only for
the XML data format for the outgoing.</p><p>A key without a prefix is a common
key for all situations.</p><p>From <strong>Camel 2.17</strong>: the options
value can use the <strong><code>#</code></strong> notation to refer to a
bean to lookup in the <a shape="rect"
href="registry.html">Registry</a></p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>corsHeaderProperty</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"> </td><td colspan="1" rowspan="1"
class="confluenceTd"><p>Allows to configure custom CORS
headers.</p></td></tr></tbody></table></div><p> </p><p><span
style="line-height: 1.4285715;">For examp
le to configure to use the spark-rest component on port
<strong><code>9091</code></strong>, then we can do as follows:</span></p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[restConfiguration().component("spark-rest").port(9091).componentProperty("foo",
"123");]]></script>
</div></div><p><span style="line-height: 1.4285715;"><br
clear="none"></span></p><p><span style="line-height: 1.4285715;">And with XML
DSL</span></p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<restConfiguration
component="spark-rest" port="9091">
<componentProperty key="foo" value="123"/>
</restConfiguration>]]></script>
-</div></div><p><span style="line-height: 1.4285715;"><br
clear="none"></span></p><p>You can configure properties on these
levels. </p><ul><li>component - Is used to set any options on the
Component class. You can also configure these directly on the
component.</li><li>endpoint - Is used set any option on the endpoint level.
Many of the Camel components has many options you can set on endpoint
level.</li><li>consumer - Is used to set any option on the consumer level. Some
components has consumer options, which you can also configure from endpoint
level by prefixing the option with "consumer." </li><li>data format - Is
used to set any option on the data formats. For example to enable pretty print
in the json data format.</li><li>cors headers - If cors is enabled, then custom
CORS headers can be set. See below for the default values which are in used. If
a custom header is set then that value takes precedence over the default
value.</li></ul><p>You can set multiple options of
the same level, so you can can for example configure 2 component options, and
3 endpoint options etc.</p><p> </p><h3
id="RestDSL-EnablingordisablingJacksonJSONfeatures">Enabling or disabling
Jackson JSON features</h3><p><strong>Available as of Camel
2.15</strong></p><p>When using JSON binding you may want to turn specific
Jackson features on or off. For example to disable failing on unknown
properties (eg json input has a property which cannot be mapped to a POJO) then
configure this using the dataFormatProperty as shown below:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<restConfiguration
component="spark-rest" port="9091">
+ <componentProperty key="foo" value="123"/>
+</restConfiguration>]]></script>
+</div></div><p><span style="line-height: 1.4285715;"><br
clear="none"></span></p><p>You can configure properties on these
levels. </p><ul><li>component - Is used to set any options on the
Component class. You can also configure these directly on the
component.</li><li>endpoint - Is used set any option on the endpoint level.
Many of the Camel components has many options you can set on endpoint
level.</li><li>consumer - Is used to set any option on the consumer level. Some
components has consumer options, which you can also configure from endpoint
level by prefixing the option with "consumer." </li><li>data format - Is
used to set any option on the data formats. For example to enable pretty print
in the JSON data format.</li><li>cors headers - If cors is enabled, then custom
CORS headers can be set. See below for the default values which are in used. If
a custom header is set then that value takes precedence over the default
value.</li></ul><p>You can set multiple options of
the same level, so you can can for example configure 2 component options, and
3 endpoint options etc.</p><p> </p><h3
id="RestDSL-EnablingorDisablingJacksonJSONFeatures">Enabling or Disabling
Jackson JSON Features</h3><p><strong>Available as of Camel
2.15</strong></p><p>When using JSON binding you may want to turn specific
Jackson features on or off. For example to disable failing on unknown
properties e.g., JSON input has a property which cannot be mapped to a POJO,
then configure this using
the <strong><code>dataFormatProperty</code></strong> as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.json)
.dataFormatProperty("json.in.disableFeatures",
"FAIL_ON_UNKNOWN_PROPERTIES");]]></script>
</div></div><p>You can disable more features by separating the values using
comma, such as:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
.dataFormatProperty("json.in.disableFeatures",
"FAIL_ON_UNKNOWN_PROPERTIES,ADJUST_DATES_TO_CONTEXT_TIME_ZONE");]]></script>
-</div></div><p>Likewise you can enable features using the enableFeatures such
as:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+</div></div><p>Likewise you can enable features using
the <strong><code>enableFeatures</code></strong> such as:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[restConfiguration().component("jetty").host("localhost").port(getPort()).bindingMode(RestBindingMode.json)
.dataFormatProperty("json.in.disableFeatures",
"FAIL_ON_UNKNOWN_PROPERTIES,ADJUST_DATES_TO_CONTEXT_TIME_ZONE")
.dataFormatProperty("json.in.enableFeatures",
"FAIL_ON_NUMBERS_FOR_ENUMS,USE_BIG_DECIMAL_FOR_FLOATS");]]></script>
-</div></div><p>The values that can be used for enabling and disabling features
on Jackson are the names of the enums from the following three Jackson
classes</p><ul><li>com.fasterxml.jackson.databind.SerializationFeature</li><li>com.fasterxml.jackson.databind.DeserializationFeature</li><li>com.fasterxml.jackson.databind.MapperFeature</li></ul><p> </p><p>The
rest configuration is of course also possible using XML DSL</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
+</div></div><p>The values that can be used for enabling and disabling features
on Jackson are the names of the enums from the following three Jackson
classes</p><ul><li><strong><code>com.fasterxml.jackson.databind.SerializationFeature</code></strong></li><li><strong><code>com.fasterxml.jackson.databind.DeserializationFeature</code></strong></li><li><strong><code>com.fasterxml.jackson.databind.MapperFeature</code></strong></li></ul><p> </p><p>The
rest configuration is of course also possible using XML DSL</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<restConfiguration
component="jetty" host="localhost" port="9090"
bindingMode="json">
<dataFormatProperty key="json.in.disableFeatures"
value="FAIL_ON_UNKNOWN_PROPERTIES,ADJUST_DATES_TO_CONTEXT_TIME_ZONE"/>
<dataFormatProperty key="json.in.enableFeatures"
value="FAIL_ON_NUMBERS_FOR_ENUMS,USE_BIG_DECIMAL_FOR_FLOATS"/>
</restConfiguration>]]></script>
-</div></div><p> </p><h3 id="RestDSL-DefaultCORSheaders">Default CORS
headers</h3><p><strong>Available as of Camel 2.14.1</strong></p><p>If CORS is
enabled then the follow headers is in use by default. You can configure custom
CORS headers which takes precedence over the default value.</p><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh">Key</th><th colspan="1" rowspan="1"
class="confluenceTh">Value</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>Access-Control-Allow-Origin</p></td><td colspan="1"
rowspan="1" class="confluenceTd">*</td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>Access-Control-Allow-Methods</p></td><td colspan="1"
rowspan="1" class="confluenceTd">GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS,
CONNECT, PATCH</td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>Access-Control-Allow-Headers</p></td><td colspan="1"
rowspan="1" class="confluenceTd">Origin, Ac
cept, X-Requested-With, Content-Type, Access-Control-Request-Method,
Access-Control-Request-Headers</td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p>Access-Control-Max-Age</p></td><td colspan="1"
rowspan="1"
class="confluenceTd">3600</td></tr></tbody></table></div><p> </p><h3
id="RestDSL-Definingacustomerrormessageas-is">Defining a custom error message
as-is</h3><p>If you want to define custom error messages to be sent back to the
client with a HTTP error code (eg such as 400, 404 etc.) then
from <strong>Camel 2.14.1</strong> onwards you just set a header with the
key <code>Exchange.HTTP_RESPONSE_CODE</code> to the error code (must be
300+) such as 404. And then the message body with any reply message, and
optionally set the content-type header as well. There is a little example shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json);
- // use the rest DSL to define the rest services
- rest("/users/")
-
.post("lives").type(UserPojo.class).outType(CountryPojo.class)
- .route()
- .choice()
- .when().simple("${body.id} < 100")
- .bean(new UserErrorService(),
"idToLowError")
- .otherwise()
- .bean(new UserService(),
"livesWhere");]]></script>
-</div></div><p>In this example if the input id is a number that is below 100,
we want to send back a custom error message, using the UserErrorService bean,
which is implemented as shown:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p> </p><h3 id="RestDSL-DefaultCORSHeaders">Default CORS
Headers</h3><p><strong>Available as of Camel 2.14.1</strong></p><p>If CORS is
enabled then the follow headers is in use by default. You can configure custom
CORS headers which takes precedence over the default value.</p><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh">Key</th><th colspan="1" rowspan="1"
class="confluenceTh">Value</th></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>Access-Control-Allow-Origin</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><strong><code>*</code></strong></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Access-Control-Allow-Methods</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><strong><code>GET</code></strong>,
<strong><code>HEAD</code>, <code>POST</code></strong>,
<strong><code>PUT</code></strong>, <strong><code>DELETE</code>, <code>TRACE<
/code></strong>, <strong><code>OPTIONS</code>,
<code>CONNECT</code></strong>, <strong><code>PATCH</code></strong></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Access-Control-Allow-Headers</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><strong><code>Origin</code></strong>,
<strong><code>Accept</code>, <code>X-Requested-With</code></strong>,
<strong><code>Content-Type</code></strong>,
<strong><code>Access-Control-Request-Method</code></strong>, <strong><code>Access-Control-Request-Headers</code></strong></td></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>Access-Control-Max-Age</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><strong><code>3600</code></strong></td></tr></tbody></table></div><p> </p><h3
id="RestDSL-DefiningaCustomErrorMessageAs-is">Defining a Custom Error Message
As-is</h3><p>If you want to define custom error messages to be sent back to the
client with a HTTP error code e.g., s
uch as <strong><code>400</code>, <code>404</code></strong> etc., then
from <strong>Camel 2.14.1</strong>: you just set a header with the
key <strong><code>Exchange.HTTP_RESPONSE_CODE</code></strong> to the error
code (must be 300+) such as 404. And then the message body with any reply
message, and optionally set the content-type header as well. There is a little
example shown below:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
+<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json);
+
+// use the rest DSL to define the rest services
+rest("/users/")
+ .post("lives").type(UserPojo.class).outType(CountryPojo.class)
+ .route()
+ .choice()
+ .when().simple("${body.id} < 100")
+ .bean(new UserErrorService(), "idToLowError")
+ .otherwise()
+ .bean(new UserService(),
"livesWhere");]]></script>
+</div></div><p>In this example if the
input <strong><code>id</code></strong> is a number that is below 100, we
want to send back a custom error message, using
the <strong><code>UserErrorService</code></strong> bean, which is
implemented as shown:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[public class UserErrorService {
public void idToLowError(Exchange exchange) {
exchange.getIn().setBody("id value is too low");
@@ -281,44 +283,43 @@ public class UserPojo {
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
}
}]]></script>
-</div></div><p>In the UserErrorService bean we build our custom error message,
and set the HTTP error code to 400. This is important, as that tells rest-dsl
that this is a custom error message, and the message should not use the output
pojo binding (eg would otherwise bind to CountryPojo).</p><h3
id="RestDSL-CatchingJsonParserExceptionandreturningacustomerrormessage">Catching
JsonParserException and returning a custom error
message</h3><p>From <strong>Camel 2.14.1</strong> onwards you return a
custom message as-is (see previous section). So we can leverage this with Camel
error handler to catch JsonParserException, handle that exception and build our
custom response message. For example to return a HTTP error code 400 with a
hardcoded message, we can do as shown below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>In the <strong><code>UserErrorService</code></strong> bean
we build our custom error message, and set the HTTP error code to
<strong><code>400</code></strong>. This is important, as that
tells <strong><code>rest-dsl</code></strong> that this is a custom error
message, and the message should not use the output POJO binding e.g., would
otherwise bind to <strong><code>CountryPojo</code></strong>.</p><h3
id="RestDSL-ReturningaCustomErrorMessageforJsonParserException">Returning a
Custom Error Message for
<code>JsonParserException</code></h3><p>From <strong>Camel
2.14.1</strong>: you return a custom message as-is (see previous section). So
we can leverage this with Camel error handler to catch
<strong><code>JsonParserException</code></strong>, handle that exception and
build our custom response message. For example to return a HTTP error
code <strong><code>400</code></strong> with a hard-coded message, we can
do as shown below:</p><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[onException(JsonParseException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
.setBody().constant("Invalid json data");
-
]]></script>
-</div></div><p> </p><h3 id="RestDSL-ParameterdefaultValues">Parameter
default Values</h3><p>You can specify default values for parameters in the
rest-dsl, such as the verbose parameter below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p> </p><h3 id="RestDSL-ParameterDefaultValues">Parameter
Default Values</h3><p>You can specify default values for parameters in the
<strong><code>rest-dsl</code></strong>, such as the verbose parameter
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ rest("/customers/")
.get("/{id}").to("direct:customerDetail")
.get("/{id}/orders")
.param().name("verbose").type(RestParamType.query).defaultValue("false").description("Verbose
order details").endParam()
.to("direct:customerOrders")
.post("/neworder").to("direct:customerNewOrder");]]></script>
-</div></div><p>From <strong>Camel 2.17</strong> onwards then the default
value is automatic set as header on the incoming
Camel <code>Message</code>. So if the call
the <code>/customers/id/orders</code> do not include a query parameter
with key <code>verbose</code> then Camel will now include a header with
key <code>verbose</code> and the value <code>false</code> because it
was declared as the default value. This functionality is only applicable for
query parameters.</p><h3
id="RestDSL-IntegratingaCamelcomponentwithRestDSL">Integrating a Camel
component with Rest DSL</h3><p>Any Apache Camel component can integrate with
the Rest DSL if they can be used as a REST service (eg as a REST consumer in
Camel lingo). To integrate with the Rest DSL, then the component should
implement the <code>org.apache.camel.spi.RestConsumerFactory</code>. The
Rest DSL will then invoke the <code>createConsumer</code> method when it
setup the Camel routes from the def
ined DSL. The component should then implement logic to create a Camel consumer
that exposes the REST services based on the given parameters, such as path,
verb, and other options. For example see the source code for camel-restlet,
camel-spark-rest.</p><h3 id="RestDSL-SwaggerAPI">Swagger API</h3><p>The Rest
DSL supports <a shape="rect" href="swagger-java.html">Swagger Java</a> by
the <code>camel-swagger-java</code> module. See more details at  <a
shape="rect" href="swagger-java.html">Swagger</a> and
the <code>camel-swagger-java</code> example from the Apache Camel
distribution.</p><p>From <strong>Camel 2.16</strong> onwards you can
define each parameter fine grained with details such as name, description, data
type, parameter type and so on, using the <param>. For example to define
the id path parameter you can do as shown below:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>From <strong>Camel 2.17</strong>: the default value is
automatically set as a header on the incoming
Camel <strong><code>Message</code></strong>. So if the call
the <strong><code>/customers/id/orders</code></strong> do not include a
query parameter with key <strong><code>verbose</code></strong> then Camel
will now include a header with
key <strong><code>verbose=false</code></strong> because it was declared as
the default value. This functionality is only applicable for query
parameters.</p><h3
id="RestDSL-IntegratingaCamelComponentwithRestDSL">Integrating a Camel
Component with Rest DSL</h3><p>Any Apache Camel component can integrate with
the Rest DSL if they can be used as a REST service(e.g., as a REST consumer in
Camel lingo. To integrate with the Rest DSL, then the component should
implement
the <strong><code>org.apache.camel.spi.RestConsumerFactory</code></strong>.
The Rest DSL will then invoke the <strong><code>createConsumer</
code></strong> method when it setup the Camel routes from the defined DSL. The
component should then implement logic to create a Camel consumer that exposes
the REST services based on the given parameters, such as path, verb, and other
options. For example see the source code for
<strong><code>camel-restlet</code></strong>,
<strong><code>camel-spark-rest</code></strong>.</p><h3
id="RestDSL-SwaggerAPI">Swagger API</h3><p>The Rest DSL supports <a
shape="rect" href="swagger-java.html">Swagger Java</a> by
the <strong><code>camel-swagger-java</code></strong> module. See more
details at  <a shape="rect" href="swagger-java.html">Swagger</a> and
the <strong><code>camel-swagger-java</code></strong> example from the
Apache Camel distribution.</p><p>From <strong>Camel 2.16</strong>: you can
define each parameter fine grained with details such as name, description, data
type, parameter type and so on, using the
<strong><code><param></code></strong>. For
example to define the <strong><code>id</code></strong> path parameter you
can do as shown below:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<!-- this is a rest GET to view an user by
the given id -->
<get uri="/{id}"
outType="org.apache.camel.example.rest.User">
<description>Find user by id</description>
<param name="id" type="path" description="The id
of the user to get" dataType="int"/>
<to uri="bean:userService?method=getUser(${header.id})"/>
</get>]]></script>
-</div></div><p>And in Java DSL</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>And in Java DSL:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[.get("/{id}").description("Find
user by id").outType(User.class)
.param().name("id").type(path).description("The id of the
user to get").dataType("int").endParam()
.to("bean:userService?method=getUser(${header.id})")]]></script>
-</div></div><p>The body parameter type requires to use body as well for the
name. For example a REST PUT operation to create/update an user could be done
as:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
+</div></div><p>The body parameter type requires to use body as well for the
name. For example a REST <strong><code>PUT</code></strong> operation to
create/update an user could be done as:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<!-- this is a rest PUT to create/update
an user -->
<put type="org.apache.camel.example.rest.User">
<description>Updates or create a user</description>
<param name="body" type="body" description="The
user to update or create"/>
<to uri="bean:userService?method=updateUser"/>
</put>]]></script>
-</div></div><p>And in Java DSL</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>And in Java DSL:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[.put().description("Updates or create a
user").type(User.class)
.param().name("body").type(body).description("The user to
update or create").endParam()
.to("bean:userService?method=updateUser")]]></script>
-</div></div><p> </p><p>For an example see
the <code>examples/camel-example-servlet-rest-tomcat</code> of the
Apache Camel distribution.</p><h3 id="RestDSL-SeeAlso">See Also</h3><ul><li><a
shape="rect" href="dsl.html">DSL</a></li><li><a shape="rect"
href="rest.html">Rest</a></li><li><a shape="rect"
href="swagger-java.html">Swagger Java</a></li><li><a shape="rect"
href="spark-rest.html">Spark-rest</a></li><li><a shape="rect"
href="how-do-i-import-rests-from-other-xml-files.html">How do I import rests
from other XML files</a></li></ul></div>
+</div></div><p> </p><p>For an example see
the <strong><code>examples/camel-example-servlet-rest-tomcat</code></strong> of
the Apache Camel distribution.</p><h3 id="RestDSL-SeeAlso">See
Also</h3><ul><li><a shape="rect" href="dsl.html">DSL</a></li><li><a
shape="rect" href="rest.html">Rest</a></li><li><a shape="rect"
href="swagger-java.html">Swagger Java</a></li><li><a shape="rect"
href="spark-rest.html">Spark-rest</a></li><li><a shape="rect"
href="how-do-i-import-rests-from-other-xml-files.html">How do I import rests
from other XML files</a></li></ul></div>
</td>
<td valign="top">
<div class="navigation">