[3/4] Docker: SpringBoot, Hibernate & Web API

In previous episodes (Part1, Part2) we saw how to create Java Maven project from scratch using SpringBoot – followed by how to deploy the application to docker and then I also demonstrated how can you set up MySQL database server, with automated initialization of a fresh database instance.

Now, let’s see how to set up RESTful Web API to display data from database using Hibernate ORM. We’ll also see how to set up a local development environment and docker deployment environment to quickly switch between the two, and establish an efficient work-flow for your project. Next, we’d set up our project to perform CRUD operations using Hibernate. And finally, we will create our Web API endpoints which can serve the requests made from the browser, in JSON format.

Let’s start by cleaning up the docker workspace (optional step).

Following commands delete images and containers.

You have been warned.

For now, we want to create two environments or profiles, a local profile for your local development and container profile for your docker container. Evidently, you can create as many profiles for different environments as you need. e.g. QA, UAT, Staging etc.

Here is my current directory structure

├── database
│   ├── 01_schema.sql
│   └── 02_data.sql
├── docker-compose.yml
├── epidemicsweb.iml
├── pom.xml
├── src
│   ├── Docker
│   │   └── Dockerfile
│   ├── main
│   │   ├── java
│   │   │   └── edu
│   │   │   └── gatech
│   │   │   └── epidemics
│   │   │   ├── api
│   │   │   │   └── HomeController.java
│   │   │   ├── edu
│   │   │   │   └── gatech
│   │   │   │   └── epidemics
│   │   │   └── EpidemicsApplication.java
│   │   └── resources
│   └── test
│   └── java
└── target

Under ./src/main/resources folder, let’s create 3 different files as following:

├── application-container.properties
├── application-local.properties
└── application.properties

Here in this example we want to create two environments or profiles, a local profile for your local development and container profile for your docker container, but you can create profiles for your different environments like QA, UAT, Staging etc. as many as you please.

(I dumped the content of these 3 files in one code-block below)

To be able to read these configurations, we need to add a maven dependency, as following:

To actually, read the configurations, let’s create a class in package edu.gatech.epidemics

Next, we need to verify if we can read the configurations:

And it works!

  • local (from my IDE)
    See Line# 16 below
  • container (from docker terminal)

Now let’s move towards the 2nd part of this episode, Web API and Hibernate ORM.

Say, I want to create an API for following table in MySQL database:

Now, back in our project we need to add maven dependencies. In pom.xml add following dependencies:

Create packages inside root as following:

  • edu.gatech.epidemics.dao
  • edu.gatech.epidemics.model
  • edu.gatech.epidemics.service

To avoid any pain, I recommend that you follow the project structure I am going for. It certainly makes things easier.

First let’s create our entity or model class as following:

Next is, our repository interface, and yes, it is actually that simple! Just be sure to extend CrudRepository.

Finally, we want to create our service as following:

Now, I do not have MySQL database server installed locally, so I am going to use guess what, a Docker container, instead! We’d wait for MySQL to be up and spinning. The wait time should not be too bad. Now let’s run the application and it works!

One more thing …

Let’s try something real quick. I would stop the application from my IDE and I would also stop the MySQL container. If you have installed the database on your local MySQL database instance, you can follow along by temporarily stopping MySQL service, to make your database temporarily inaccessible.

You can even repeat the same test with empty database (i.e. no tables) and your SpringBoot application would still fail to start.

This proves that before it starts, Hibernate from SpringBoot is expecting MySQL database to be reachable and the database schema to be in place.

So we must ensure the same for our docker configuration as well! To achieve our goal, we need to create a shell script file in our resources folder, along with the 3 files we just created, and let’s call it wrapper.sh

This also requires slight modification in our existing Dockerfile as following:

Now, let’s go ahead and rebuild our project.

Kwel! We are now ready to create our API controller ./src/main/java/edu/gatech/epidemics/api/UserApiController.java as following and run the project one final time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s