Defining Services using REST DSL (REST)

This is effectively a facade that provides a simplified syntax for defining REST services in a Java DSL or an XML DSL (Domain Specific Language). The REST DSL does not provide the REST implementation, it is just a wrapper around an existing REST implementation.

Advantages of the REST DSL

The REST DSL wrapper layer offers these advantages:

  • Modern easy-to-use syntax for defining REST services.

  • Compatible with multiple different Apache Camel components.

  • Swagger integration

Configure REST DSL with REST Implementation

To specify the REST implementation, we can either use the restConfiguration() builder (in Java DSL) or the restConfiguration element (in XML DSL). For example, to configure REST DSL to use the Spark-Rest component, you would use a builder expression like the following in the Java DSL:

restConfiguration().component("spark-rest").port(9091);

Or, use the following (as a child of camelContext) in the XML DSL:

<restConfiguration component="spark-rest" port="9091"/>

REST DSL with Java

In Java, to define a service with the REST DSL, we can place the REST definition into the body of a RouteBuilder.configure() method (just like we can do for regular Apache Camel routes). For example, to define a simple Hello World service using the REST DSL with the Spark-Rest component:

restConfiguration().component("spark-rest").port(9091); rest("/say") .get("/hello").to("direct:hello") .get("/bye").to("direct:bye"); from("direct:hello") .transform().constant("Hello World"); from("direct:bye") .transform().constant("Bye World");

The prior REST DSL sample features three different kinds of builders:

restConfiguration()

Configures the REST DSL to use a specific REST implementation (Spark-Rest).

rest()

Defines a service using the REST DSL. Each of the verb clauses is terminated by a to() keyword, which forwards the incoming message to a direct endpoint (the direct component splices routes together within the same application).

from()

Defines a regular Camel route.

REST DSL with XML

To define a service with the XML DSL, we can define a rest element as a child of the camelContext element. For example, we can define a simple Hello World service using the REST DSL with the Spark-Rest component:

REST DSL and HTTP Transport Component

If we do not explicitly configure an HTTP transport component, then the REST DSL will automatically discover which HTTP component to use by checking for components on the classpath. The REST DSL searches for the default names of any HTTP components, it then uses the first one it finds. If there are no HTTP components on the classpath, and we did not explicitly configure an HTTP transport, then the default HTTP component is camel-http.

Specifying the content type of requests and responses

You can filter the content type of HTTP requests and responses using the consumes() and produces() options in Java, or the consumes and produces attributes in XML. The most common content types (officially known as Internet media types) are the following:

  • text/plain

  • text/html

  • text/xml

  • application/json

  • application/xml

The content type is specified as an option on a verb clause in the REST DSL:

In XML, you can set the consumes and produces attributes, as follows: