Site icon JVM Advent

Helping Santa Claus with a Spring Boot microservice

With Bootify you can initialize a Spring Boot application with your own database schema and REST API. In this article we want to create a small example backend that allows Santa Claus to manage the delivery of his presents. How else is he supposed to keep track of the millions of children?

Santa Claus tracking the visits on his iPhone

First steps and database schema

We begin on Bootify.io by clicking on Start Project. The project gets its own URL, which we may bookmark for future reference.

Now we are in the general settings where we want to keep Gradle as a build tool. Santa’s datacenter is working with MySQL and Java8, so we make this two choices. Lombok saves us a lot of Getters and Setters, so we activate this option too.

Next we can create our schema in the tab “Entities”. The finished schema is shown later on, but at first we start with the table “Child”. Besides the primary key, this table has a “name” (unique String with 1000 characters, so that every child can be identified), a “dateOfBirth” (type LocalDate) and “didBehave” (type Boolean) – not every child gets a gift.

Creating our first table in Bootify

After that we come to the second table “Present”. There is only a limited number of gifts that are actually wishable, divided by age groups. Therefore we’re including the custom fields minAge, maxAge (type Integer) and description (type Text).

Second table for the presents produced at Santa’s workshop

Santa Claus is going to track the visits via his iPhone, for which we add the third table “PresentDelivery”. Here there is only the extra field sungSong (String), but after creating the table we insert two more relations describing our foreign keys.

Creating our foreign keys

PresentDelivery refers to a Child in the ratio N:1. The name “child” is sufficient for us here and we want to mark the relation as required. In the same way we create the relation to the Present table. Our scheme is now complete and we can examine the result in the UML preview.

UML Preview of our final schema in Bootify

Code Review and Download

This means that we are already finished with the initial version of our microservice! Using “Explore” we can look at the generated code and see not only the JPA / Hibernate entities, but also the services and rest controllers that provide basic CRUD functionality.

Exploring the generated code of our new microservice

Honestly, one could have made the database schema a bit more precise, so that Santa Claus can manage the behaviours and presents separately for each year. If you would like to improve that, get a copy of the project on the following link and extend it as you like: https://bootify.io/helping-santa-claus

The elves are still working on the iPhone app – hopefully everything will be ready in time. Be good and have a nice christmas time!

Exit mobile version