Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Rest DSL with XML

The REST DSL supports the XML DSL also using either Spring or Blueprint. The example above can be define in XML as shown below:

Code Block
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <rest uri="/say/hello">
      <get>
        <to uri="direct:hello"/>
      </get>
    </rest>
    <rest uri="/say/bye">
      <get consumes="application/json">
        <to uri="direct:bye"/>
      </get>
      <post>
        <to uri="mock:update"/>
      </post>
    </rest>
    <route>
      <from uri="direct:hello"/>
      <transform>
        <constant>Hello World</constant>
      </transform>
    </route>
    <route>
      <from uri="direct:bye"/>
      <transform>
        <constant>Bye World</constant>
      </transform>
    </route>
  </camelContext>

 

Using path prefixes

The REST DSL allows to define path prefixes to make the DSL a bit more DRY. For example to define a customer path, we can set the prefix in rest("/customer") and then provide the past postfix in the verbs, as shown below:

Code Block
  rest("/customers/")
      .get("/{id}").to("direct:customerDetail")
      .get("/{id}/orders").to("direct:customerOrders")
      .post("/neworder").to("direct:customerNewOrder");

 

And using XML DSL it becomes:

Code Block
    <rest uri="/customers/">
      <get uri="/{id}">
        <to uri="direct:customerDetail"/>
      </get>
      <get uri="/{id}/orders">
        <to uri="direct:customerOrders"/>
      </get>
      <post uri="/neworder">
        <to uri="direct:customerNewOrder"/>
      </post>
    </rest>
Tip

The REST DSL will take care of duplicate path separators when using path prefixes. In the example above the rest path prefix ends with a slash ( / ) and the verb starts with a slash ( / ). But Apache Camel will take care of this and remove the duplicated slash.

 

 

camel-example-spark-rest-tomcat

...

To define the REST services we use the rest method, where we can setup the path, which is "/hello/{me}". Where me refers the a wildcard that is mapped to Camel message header with the same key. Notice how we can refer to this header in the embedded Camel routes where we do message transformationAnd because we used embedded routes, we need to define this using .route(), and to denote the end of the route, we use .endRest() to go back to the Rest DSL, where we can then add the 2nd, and 3rd get service. 

Configuring Rest DSL

The Rest DSL allows to configure the following options using a builder style

OptionDefaultDescription
component The Camel Rest component to use for the REST transport, such as restlet, spark-rest.
schemehttpThe scheme to use for exposing the REST service. Usually http or https is supported
hostname0.0.0.0The hostname to use for exposing the REST service.
port The port number to use for exposing the REST service.
property Allows to configure as many additional properties. This is used to configure component specific options such as for Restlet / Spark-Rest etc.

 

For example to configure to use the spark-rest component on port 9091, then we can do as follows

Code Block
restConfiguration().component("spark-rest").port(9091).property("foo", "123");

 

And with XML DSL

Code Block
    <restConfiguration component="spark-rest" port="9091">
      <restProperty key="foo" value="123"/>
    </restConfiguration>

 

 

See Also