Building Spring boot Rest API using Apache Camel and PostgreSQL.

Supun Kavinda
4 min readOct 22, 2020

--

Creating a restful web service using Spring boot, Apache Camel, and PostgreSQL(using Java Camel DSL).

I recently got an interview assignment, and it was to create a restful web service using spring boot, apache camel, and PostgreSQL. While I am completing that assignment I got some knowledge and try to share it with you.

I am not going to explain what is camel, spring, and PostgreSQL. Because there have so many tutorials and blog posts out there about those techniques. I am trying to give a basic getting started guide to start and continue the journey with this technology.

Topics

  1. How to create a maven spring boot project.
  2. Add required dependency to the pom.xml file.
  3. Add PostgreSQL configuration and Create Classes.
  4. Create camel routes for basic crud operations.

1. How to create a maven spring boot project

You can use spring initializer or IntelliJ idea to create a spring boot application. Here I will go with spring initializer.

First, go to the spring initializer page and bootstrap spring boot application by selecting the below options.

  • Project: Maven Project
  • Language: Java
  • Spring Boot version: 2.3.4
  • Group Id: com.springboot.camel.postgresql
  • Artifact: Spring-boot-camel-postgresql
  • Packaging: Jar
  • Java: 8
  • Dependencies: Spring Web, Spring Data JPA

Finally, you can press GENERATE to create a project. Then unzip and open it using your favorite IDE. I am going to use the Intellij idea ultiate version.

2. Add required dependency to the pom.xml file.

Open the pom.xml file and remove the spring boot parent pom show in below.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>

Probably you will be wondering why I am asked to remove parent dependency. I will explain the reason, But before that let's try to understand what is doing this parent pom. This parent pom responsible for managing spring boot dependencies and plugins. It contains the default version of the dependencies and default configuration of the plugins.

Here we are using both camel and spring boot. both this has a parent pom. but we can’t use two or more parent poms in the same pom file. So that’s why I asked to remove spring parent pom.

Let’s think about the way to handle this parent pom issue. We can use Spring Boot and Apache Camel BOM (Bill of Materials ) dependencies to manage this issue and those dependencies include in the DependencyManagement section of the pom file. Here is the complete pom.xml file.

pom.xml file

3. Add PostgreSQL configuration and Create Classes.

Before dealing with code let’s configure our application properties in the application.propertiesfile located in the source resource directory.

application.properties file

Note: Please change the database user name, password, and port according to your configuration. If you are not specified camel.component.servlet.mapping.contextPath value. Then it’s default value is camel.

Now We can start codding. As a first step, we can add a Book Entity class to our project. I am going to add the Book entity class under the entity package. That code will look like below.

Book.java class

The next step is creating Repository interface for the book entity. So we can create a BookRepository interface by extending JpaRepository interface. That class creates an inside respository package. It’s like below.

Then I am going to create BookService class to handle the CRUD operation. This class create inside service package.

4. Create camel routes for basic crud operations.

Now we come to the final stage of this sample project. until now we create all the required configuration and classes except the rest controller class. also, we didn't use Apache camel in any place. So I am going to create a rest controller class using camel Java DSL without using spring MVC. If we create a rest controller using camel’s route, we have to extend our rest controller class using RouteBuilder. Once we extend the rest controller from RouteBuilder we have to override configure method. We define all our routes in the configuration method like below.

Here, I create four routes to do CRUD operation. Below I include CURL request for all routes and include the postman collection in the Github with a sample project. You can use one of these methods to test this sample example.

  1. Save Book CURL request
curl -XPOST -H "Content-type: application/json" -d '{
"id":0,
"name":"Camel in Action",
"author":"Claus Ibsen and Jonathan Anstey",
"price": 56
}' 'http://localhost:8080/rest/book'

2. Find all Books CURL request

curl -XGET -H "Content-type: application/json" 'http://localhost:8080/rest/book'

3. Find Book By Name CURL request

curl -XGET -H "Content-type: application/json" 'http://localhost:8080/rest/book/Camel%20in%20Action'

4. Remove Book By bookId CURL request

curl -XDELETE -H "Content-type: application/json" 'http://localhost:8080/rest/book/1'

Now, this sample project implementation is complete. Before running this application we need to run the PostgreSQL server. Here I am using the PostgreSQL docker version.

docker run --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 --name postgresql-camel -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=cameldb -p 5432:5432 postgres:latest

If you want to know more about camel you can read camel official documentation and Camel in Action book.

Here I have only implemented GET, POST, and DELETE methods. You can try to implement PUT, and PATCH methods also.

You can find complete code in my GitHub account.

If you have any questions please put a comment I am happy to help you.

--

--

Supun Kavinda

Senior Software Engineer | Java | AWS | Angular | Reactjs | Terraform | Jenkins