Apache Camel: CRUD REST Initiation

Daniel S. Blanco
3 min readJun 15, 2020

--

We are going to initiate in the Apache Camel world with a basic example of how to do a CRUD service, but with a mock backend.

Firstly, let’s talk a little bit about Apache Camel:
- It’s an open-source integration framework.
- It allows several ways of configuration and development.
- It has a large set of components that allow integration with other software.
- It can be integrated and deployed with the help of Spring boot, Quarkus, standalone, etc.
- It allows transformation easily from one format to another.

In this post, we will see how to make a simple REST service with Camel, Spring Boot, and REST DSL support. Camel has several DSL (domain-specific language), which allows us to configure, route, or generate the REST service. Creating entry points, performing a certain logic, and returning a relevant value.

The routing main class will extend from RouteBuilder and must overwrite its configure method. Within that method we’ll have to perform two main tasks:
1. Configure the operation of the REST services through Apache Camel. Indicating the component to use (servlet, http-netty, jetty, etc), what to produce (through bindingMode), and other details.
2. We will also create the entry points (through the rest() method) and its configuration.
2.1 With the get() method, we indicate that we are going to enable a service associated with an HTTP GET method and with the book context.
2.2 With the produces() method we indicate that we are going to produce.
2.3 With the router() method we are going to redirect the flow.
2.4 With the bean() method we can associate the message to be treated by a specific object and method.

Next, we can see an example of how it’s working when we invoke it:

We are going to see how to do the GET method for a particular object. But in this case, it will return an XML object.

Here we have two new utility methods:
- description() which allows us to associate a description not only to the method itself but also to the parameters. Useful for documenting the associated swagger with more detail.
- outType() which allows us to indicate the type of response object.

We can also see how to pass the path params to the backend method, through ${header}. And how to return the response in XML format. For this last one, we must take into account two aspects:
- We have to indicate as a dependency the camel-jaxb library (as for JSON we associate camel-jackson).
- The output object must contain JAXB annotations.

Now we’ll see a POST method and how we can log a message:

This post method will have an associated route identifier and we can pass the payload through the ${body} parameter to the backend method.

Turn for the PUT method, in which we can validate the input parameters through simple() and otherwise() methods. And how we can return an appropriate response through the Exchange object.

And finally the DELETE method

If you are more interested, you can see all the code here.

--

--

Responses (1)