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:
<camelContext xmlns="http://camel.apache.org/schema/blueprint"> <restConfiguration component="spark-rest" port="9091"/> <rest path="/say"> <get uri="/hello"> <to uri="direct:hello"/> </get> <get uri="/bye"> <to uri="direct:bye"/> </get> </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>