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:
...
Code Block |
---|
<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:
...
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:
Code Block |
---|
<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> |
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:
...