Versions Compared

Key

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

...

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

We provide an example that uses the REST DSL with the Spark Rest component that can be deployed in web containers such as Apache Tomcat. This example defines a single REST service which offers a GET operation that accepts 3 different types: plain text, json, and xml.

Embedding Camel routes

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. 

There are two route modes with the Rest DSL

  • mini using a singular to
  • embedding a Camel route using route 

The first example is using the former with a singular to. And that is why we end up with 3 + 2 = 5 total routes.

The same example could use embedded Camel routes, which is shown below:

Code Block
languagejava
    protected RouteBuilder createRouteBuilder() throws Exception 
Code Block
public class MySparkRouteBuilder extends RouteBuilder {
    @Override
    publicreturn voidnew configureRouteBuilder() throws Exception {

 {
            @Override
    // configure we want to use spark-rest as thepublic componentvoid forconfigure() thethrows restException DSL{
        restConfiguration().component("spark-rest");

        rest("/say/hello")
 use the rest DSL to define rest services, and use embedded routes
        rest("/hello/{me}")
.get().route().transform().constant("Hello World");
                .get().consumes("text/plainrest("/say/bye")
                    .get().consumes("application/json").route().transform().constant("Bye World").endRest()
                    .post().to("logmock:inputupdate");
        };
        }

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 go back to the Rest DSL and continue defining REST services.

Tip
titleConfiguring route options

In the embedded route you can configure the route settings such as routeId, autoStartup and various other options you can set on routes today.

.get().route().routeId("myRestRoute").autoStartup(false).transform().constant("Hello World");

 Managing Rest services

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. This makes it the same 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.

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.

Example - camel-example-spark-rest-tomcat

We provide an example that uses the REST DSL with the Spark Rest component that can be deployed in web containers such as Apache Tomcat. This example defines a single REST service which offers a GET operation that accepts 3 different types: plain text, json, and xml.

public class MySparkRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { // configure we want to use spark-rest as the component for the rest DSL restConfiguration().component("spark-rest"); // use the rest DSL to define rest services, and use embedded routes rest("/hello/{me}") .get().consumes("text/plain") .route() .to("log:input") .transform().simple("Hello

...

${header.me}").endRest()

...

.get().consumes("application/json")

...

.route()

...

.to("log:input")

...

.transform().simple("{

...

\"message\":

...

\"Hello

...

${header.me}\"

...

}").endRest()

...

.get().consumes("text/xml")

...

.route()

...

.to("log:input")

...

.transform().simple("<message>Hello

...

${header.me}</message>");

...

}

...

}

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. 

...

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

 

a is
OptionDefaultDescription
component 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 org.apache.camel.spi.RestConsumerFactory is registered in the registry. If either one is found, then that is being used.
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 for Restlet / Spark-Rest etc etc.

 

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

...

And with XML DSL

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


Integration a Camel component with Rest DSL

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 org.apache.camel.spi.RestConsumerFactory. The Rest DSL will then invoke the createConsumer 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 camel-restlet, camel-spark-rest.

 

See Also

DSL

Rest

Restlet

Spark-rest