Kotlin Spring Boot Tutorial Part 3: Creating REST endpoints for a task app

Habibi Coding | حبيبي كودنق
Geek Culture
Published in
6 min readJan 15, 2023
tutorial banner

Here is a quick summary of Part 2:

Classes were added for exception handling. The service layer with actual logic and the use of the exception classes were implemented. The TaskController was created with all endpoints. Last but not least an extra class for CORS configuration was created, so when the project gets deployed in later tutorials we do not run into problems. Here is a link to part 2.

Connect to an actual database

It is time to attach a real database to our API so we see if our CRUD operations work properly.

Adapt application.yml

Navigate to main -> resources and change application.properties to application.yml. Because a YAML file gives a better overview of the actual application configuration.

logging:
file:
name: app.log
server:
error:
include-binding-errors: always
include-message: always
include-stacktrace: on_param
port: ${SERVER_PORT:8080}
spring:
datasource:
username: ${DATASOURCE_USERNAME:sa}
password: ${DATASOURCE_PASSWORD:''}
url: ${DATASOURCE_URL:jdbc:h2:mem:testdb}
driver-class-name: ${DATASOURCE_DRIVER_CLASS_NAME:org.h2.Driver}
jpa:
database: ${JPA_DATABASE:h2}
hibernate:
ddl-auto: update
properties:
hibernate:
jdbc:
lob:
non_contextual_creation: true
show-sql: true

The logging of the backend should happen in a file called app.log . errors should always be included in binding errors and the error message as well. The stack trace only when the parameter is passed for instance http://localhost:9091/api/all-tasks?trace=true. The port on which the application runs I want to hide for security purposes so I say use the environment variable called SERVER_PORT if there is no environment variable use the default value 8080. Same for the properties username, password, URL and driver-class-name indatasource. As well as for the property database in jpa.

Later these env variables will be passed to the docker container. But for now, we can define it in our IntelliJ IDE env variables. Just go to Edit Configuration…

Edit Configurations…

Then on the right side, you should find Modify Options

Modify options

Select in the section “Operating system” Environment variables

Environment variables

Now you should see in the “Edit configuration” screen the new input field “Environment variables: “ click on the icon on the right side of the input field which says Edit Environment variables.

Edit Environment variables

Click on the + plus icon to add the env variables

Add env variables

Then add all six env variables we defined in application.yml . Just for now add “test” as a value for all environment variables. We will change it after we set up the actual database.

env variables from application.yml

Set up the actual database

Go to https://supabase.com/ to set up a FREE PostgreSQL database for our project. After a successful sign in go and “Start a new project”. When setting up your project make sure FREE PLAN is selected. Make sure you remember the password entered, we will need it to connect to the database. When your project is set up, click on the left side of the gear icon (Settings) then on Database.

database settings

On this page, you will find all the necessary information to connect to your database.

You can switch now again to IntelliJ IDE. On the right side of the IDE you will find the ribbon Database click on the + plus icon and Data Source -> PostgresSQL. Enter in the new window all information from Supabase “Contact info”. After entering all values click on “Test connection” to make sure you entered everything properly. Later we can check with Query Console if our queries were successful.

Add Postgres database

After you saw the “Test connection” was successful, copy the URL input value somewhere like in another text editor because we will need it in a few seconds.

Running the project for the first time

Open again the “Edit Configurations…” in your IntelliJ IDE.

env variables

In this window edit, all values where we put “test” with your actual values from Supbase “Contact info”. For DATASOURCE_URL put the copied value from before instead of test. For DATASOURCE_DRIVER_CLASS_NAME put “org.postgresql.Driver”. For JPA_DATABASE put “postgresql”. For SERVER_PORT put “9091”. For DATASOURCE_PASSWORD & DATASOURCE_USERNAME put your values from Supabase Database.

Now start your application! Yalla | يلا

Then you should see something like this.

Console output from running application

After that open again the Database ribbon on the right side and click on refresh for your Postgres database. Then you should be able to see a task table.

refresh database

Next click on the task table and click on the Query Console icon and select Open Default Console.

Query Console

Write inside the console:

SELECT * FROM task; 

You should get 0 entries.

first select query

Add your first entry

Open the HTTP client of your choice I will be using Postman. Add the POST endpoint:

http://localhost:9091/api/create

Then add for the body following JSON object:

{
"description": "Buy Shawarma / شراء شاورما",
"isReminderSet": false,
"isTaskOpen": true,
"createdOn": "2022-12-10T01:06:32.510",
"priority": "MEDIUM"
}

Your entered values should look like these

Postman

Then you should get a 200 OK, open again the Query Console in IntelliJ IDE and execute again the `select` query from before.

the first entry in the database

You should see now one value. But before continuing to test the other endpoints let us create one more JSON object. Call again the POST endpoint with this object:

{
"description": "Feed the cat / أطعم القط",
"isReminderSet": false,
"isTaskOpen": false,
"createdOn": "2022-12-09T01:06:32.510",
"priority": "HIGH"
}

Open any web browser and enter the endpoint GET endpoint

http://localhost:9091/api/all-tasks

Then you should see something like this:

/all-tasks response

Then try to call the GET endpoint /task/2 and the result should be:

/task/{id} endpoint

Okay, I think the other GET endpoints you will be able to test them on your own.

Update and delete a task

First, let us test the PUT endpoint /update . I modified the JSON object from before and it looks like this now:

{
"id": 2,
"description": "Feed the other cat / أطعم القط الآخر",
"isReminderSet": true,
"isTaskOpen": true,
"createdOn": "2022-12-12T01:06:32.510",
"priority": "HIGH"
}

The description, isReminderSet, isTaskOpen and createdOn got modified. When you get a 200 OK response you can also check it in the browser.

updating a task with Postman

Lastly, we want to delete a task so our CRUD operations are complete. Call the DELETE endpoint /delete/{id} . So, let us delete the first task by calling

http://localhost:9091/api/delete/1
delete task endpoint

If you try to delete the first task AGAIN you should see this:

Task with ID: 1 does not exist!

Okay, that’s it for the third part. If you enjoyed this article give it a clap. Here is Part 4:

Here is the completed project, check out the branch part_three

By the way here is the link for the article as YouTube series: https://www.youtube.com/watch?v=ZKMGMZqnmOk&list=PLjuEK3Ez60n2dTFL7-KETl1yl04kOo-rM

--

--