How to expose a REST endpoint with Camel

In my previous post, I wrote an introduction to Apache Camel telling what Apache Camel is and what its goals are. Now, I’m posting an example exposing the REST routes. I selected this theme because it is very common for developers, and the REST are used a lot to open your application to communication with outside worlds. I’ll not explain in deep about the REST, as it is not my goals here, but I’ll explain the way to expose REST route using Camel.

REST (Representational State Transfer) is a popular architectural style used in web applications. Camel is a lightweight and powerful integration framework that can be used to implement RESTful services. Thus, let’s go to implement that!

First, you’ll need to create a simple Java project. To do that, you will use the following Maven command:

mvn archetype:generate -DgroupId=net.rhuanrocha -DartifactId=restsample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Note that you are creating a project called restsample.

Now you will add the following dependencies to pom.xml:

<dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-main</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-rest</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-netty-http</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jackson</artifactId>
      <version>${camel.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.6</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>2.0.6</version>
    </dependency>
  </dependencies>

The camel version used was 4.0.0-M1

Now, you’ll use the camel-main to start the Camel context and route. The Main class in Camel is a utility class that provides a convenient way to start and stop Camel contexts. It can be used to start a Camel route in a standalone application or to integrate Camel with other frameworks, such as Spring or Karaf. You can read more about Camel Rutimes in this post written by Raymond Meester.

public class App 
{
    public static void main( String[] args ) throws Exception {
        Main main = new Main(App.class);
        main.run(args);
    }
}

Now, you’ll need to create a Camel route that exposes the REST endpoint. Here’s an example of a simple route that exposes a GET endpoint:

import org.apache.camel.builder.RouteBuilder;

public class RestRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        restConfiguration()
                .component("netty-http")
                .host("0.0.0.0")
                .port(8080);

        rest("/hello")
                .get()
                .to("direct:hello");

        from("direct:hello")
                .setBody(simple("Hello, ${header.name}!"));
    }
}

In this example, the rest:get:/hello endpoint is exposed. When a GET request is made to this endpoint, the route’s logic is executed, and a response containing the string “Hello, ${header.name}!” is returned. The name is passed as query parameter on the request. Note we are using the camel-netty-http component, but you can use camel-jetty, camel-servlet, or camel-undertow.

You can customize the response further by using Camel’s message headers. For example, you can set the HTTP status code, response headers, and response body. Here’s an example of a route that sets a custom HTTP status code and response header:

In this example, the Exchange.HTTP_RESPONSE_CODE header is set to 200, indicating that the request was successful. Additionally, a custom header called “Custom-Header” is set with the value “Custom Value”.

        rest("/hello")
                .get()
                .to("direct:hello");

        from("direct:hello")
                .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
                .setHeader("Custom-Header", constant("Custom Value"))
                .setBody(simple("Hello, ${header.name}!"));

In summary, exposing a REST endpoint with Camel is easy and flexible. You can customize the response by using Camel’s message headers and data formats, and you can define custom request and response objects to represent the data being sent and received. You can check the completely sample in this repository in my GitHub. In my next post I’ll show you how you can expose the same Rest endpoint using the Quarkus as the runtime.

Leave a comment

Create a website or blog at WordPress.com

Up ↑