Aurora Node About

Spring APIs ft. cats!

Me and a cat chilling.

Docker, Docker, Docker!

As a precursor let's get Docker out of the way. It's funny that I always write about Docker so much but I really like the whole workflow around it. I decided to re-organize my files as it was a clutter.

It is a lot more coherent now with JetBrains projects in their respective folders and compose files along with volumes living in a folder all together. I guess I am collecting databases now.

Dolphin file explorer showcasing different directories with colorful custom icons.
Dolphin with Custom Icons

Initially for the spring app, there was H2 usage but ultimately I wanted to deploy the app so I migrated to PostgreSQL in the end. Additionally, it will prove useful in the future as I will be using PostgreSQL and possibly PostGIS for a bigger project and Heroku makes it easy to provision both.

Showcased below, is the compose file used for the PostgreSQL database along with pgAdmin. Images from Docker Hub along with the YML file are linked in the end of the post.

💡 To log-in into pgAdmin you need to provide a format of an email like so: user@test.com.

💡 The volume for pgadmin4 is optional as you can just provide the server information when you log-in into pgAdmin.

💡 I like using LazyDocker when I setup everything as it easily lets you choose what to spin up. You might not want pdAdmin.

Docker compose file for database and pgadmin.
PostgreSQL & pgAdmin4 Compose File

A POST Request in Spring

I wanted to do a quick revision for Java and Springboot so I went ahead and created a rather simple API and learned stuff that I had obviously missed when I was enrolled into my springboot course.

Below you can see an infographic I sketched using Obsidian and Excalidraw that showcases relationships between classes of code that I find useful in order to understand what exactly is going on when there is a POST request to create a new Cat. We'll take a closer look as there's a lot of stuff going on behind the scenes.

Program classes and their relationships.
Controller, Service, DTO and Mapper.

The .createCat() controller method has an annotation of @Valid that triggers Validation. Validation of what though? Let's dive into the CatRequest record and see what's in there and how validation works!

A snippet showing a record java class.
CatRequest Record Class with Validation.
Controller
CatController class
Service
CatService class

Since the Service layer has two (2) methods being used to map a request into an entity and an entity to a response let's take a look at those two methods as well.

Mapper
CatMapper class

Thoughts

Separating code to different files and packages makes everything neat and organized. When coding, it is important to have a place where each corner is responsible to do something specific. A controller should handle requests and should not be cluttered with business logic. A mapper maps. A repo repoes and so on.

Spring-wise, it is quite interesting that parts of your code are inverted in regards of control to be handled by the spring container. Paired with DI(dependency injection) you get instances of your beans where they're needed and everything becomes easier. Spring really does the heavy lifting behind the scenes!


Heroku and Deploying

Now let's deploy the API to Heroku! It's pretty simple actually and the docs are a breeze. Heroku provides 312$ worth of credits if you're a student and enrolled into the GitHub Student Developer Pack which is suprisingly easy to do so. No brainer. Let's see how we can deploy our app now.

That's all. The app is now deployed but we still do not have a PostgreSQL database so let's do that as well. Heroku makes it easy.


Testing with HTTPie

A program that is being used to test an API.
HTTPie API Testing

As you can see the POST request on /api/v1/cats responds with the new Cat we have created!


References


That's all folks. Thanks for reading!