How to: Spring Boot JPA + REST with Bootify
Tags: java, springboot, microservices, tutorial
Published: 2020-08-23 18:58:00

**With the Spring Initializr we can create a basic project structure, but [Bootify.io](https://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](https://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.

![Bootify.io General Tab](https://dev-to-uploads.s3.amazonaws.com/i/z1c1icbjyvx6zlofa90u.png)

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.

![First entity "Voucher"](https://dev-to-uploads.s3.amazonaws.com/i/6qmxq43rlnjdrg9ivxro.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/no38m8yty3x6bdk4tr1m.png)

This completes our database model. This is also shown to us immediately as an UML diagram in the preview.

![UML Preview](https://dev-to-uploads.s3.amazonaws.com/i/k319wcpn5stjqp415uhj.png)

## 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`.

![Enum "VoucherStatus"](https://dev-to-uploads.s3.amazonaws.com/i/98t6crm2f2zomvsh2ivs.png)

We can now select the new enum as the custom enum type at the `Voucher` entity in the corresponding field.

![Custom Enum Type](https://dev-to-uploads.s3.amazonaws.com/i/vcxupb5yycvhfspe4piv.png)

## 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.

![Code preview](https://dev-to-uploads.s3.amazonaws.com/i/lck2xj8tvc4eypsorwby.png)

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.

![Update database credentials](https://dev-to-uploads.s3.amazonaws.com/i/401vl7j5uyo52itjy6yd.png)

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.

![IntelliJ Run Profile](https://dev-to-uploads.s3.amazonaws.com/i/myw0kl9e0qr72qoys28u.png)

## 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.

![Our REST API via Swagger UI ](https://dev-to-uploads.s3.amazonaws.com/i/7jfkn3kcrrt8al1ucpvs.png)

I wish you much success with your new applications! Feel free to contact me for your questions on [Bootify.io](https://bootify.io).