How to: Spring Boot JPA + REST with Bootify
Thomas
Posted on August 23, 2020
With the Spring Initializr we can create a basic project structure, but Bootify.io goes one step further: we define our own database schema, with all entities accessible through a simple REST API. In this article we want to create a small example application for managing vouchers using Bootify.
Update 2022: Bootify now also provides a frontend option. Select your preferred setup with Thymeleaf and Bootstrap/TailwindCSS and enable CRUD options for your entities.
Database selection
With "Start Project" on Bootify.io a new project is created, which is stored under its own URL. It should be saved for later access. With this we are directly within the creation of our App.
On the first tab "General" there are settings like the project name, build type (Maven/Gradle) or enabling lombok. Here you can choose your database - you can find MySQL, MS SQL and Derby among others. But we stay with PostgreSQL. The schema generation can be done using Hibernate (ddl-auto: update
), but this is not recommended for production usage. Therefore we stay with liquibase and changelogs for our schema will be provided.
Further dependencies are not needed for our simple example. So apart from the name, we have not changed any of the default settings here.
Entities
Let us now come to the core of our application, the tables. In the tab "Entities" we create the first table Voucher
with the fields voucherStatus
, voucherCode
, initialValue
and currentValue
. All fields are required, whereby the voucherCode is also unique. We also change the type of our two value fields to numeric. With this information provided, we can already save our first table.
As second table we want to have a VoucherActivity
. Here we have the fields source
(string) and changeValue
(numeric). All tables come automatically with a primary key and date fields (if the option has not been disabled on the "General" tab). For this example dateCreated
should be sufficient to track the date of the activity.
For both of our tables we are keeping the "Add REST endpoints" flag checked, so a REST API with simple CRUD operations is provided.
"Voucher" and "VoucherActivity" are in an easy to understand 1:N relation, which we also adding now.
This completes our database model. This is also shown to us immediately as an UML diagram in the preview.
Enums
Since we would like to have a defined set of states for the field voucherStatus
, we create an additional enum VoucherStatus
in the tab "Data Objects". For our example this enum gets the constants VALID
, REDEEMED
and CANCELLED
.
We can now select the new enum as the custom enum type at the Voucher
entity in the corresponding field.
Preview and Download
Now we can already have a look at the current code with the "Explore" item. In the package domain
we see the entities and repositories that form the ORM layer using Spring Data and Hibernate. In the package rest
we see the REST controllers with the CRUD endpoints. In resources/changelogs
we can find the liquibase script that will generate our database schema.
If we are satisfied with the result, we can download the code and import it into our favorite IDE.
Next steps - IntelliJ
To have the new app running on our own machine, let's import the application into IntelliJ. The easiest way to do this is to use "New > Project from existing sources" and select the build.gradle
.
After that we can update the database credentials for our test system. In the application.yml we are seeing some expressions referring to environment variables (e.g. JDBC_DATABASE_PASSWORD
) with the fallback behind the colon - there we can now specify our local variables. The database your are referring to should exist in an empty state.
With this we can finally start the first version of our application. The Run Configuration could look like this. As a VM option, we have also activated the profile local
- with this we could add an application-local.yml
, for example, and put our database credentials in a separate YAML. But this is not necessary at first.
Ready to start
We can now run our application. Since we had OpenAPI activated, there is also SwaggerUI reachable at http://localhost:8080/swagger-ui.html
where we can try out our REST API.
I wish you much success with your new applications! Feel free to contact me for your questions on Bootify.io.
Posted on August 23, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.