How to Secure a Spring Boot Application with TLS
Creating a Spring Boot Application

In this section, we will create a Spring boot application and expose the following endpoints:

GET v1/books/ : List all books

POST v1/books/: Create a new book

GET v1/books/{book_id}: Get a book resource

DELETE v1/books/{book_id}: Remove a book

Step 1: Creating a Spring Boot Project

Browse to your favorite IDE and create a Spring boot project with web, h2, data-jpa and Lombok dependencies. Following is the pom.xml file:

pom.xml

Step 2: Configuring h2 database

In this application, we will use the h2 in-memory database as our backing database. Add the following configuration in the application.properties file to configure h2 database:

application.properties

Step 3: Creating the domain object

In this application, we will manage book information. Users of this application/API can perform CRUD operations. We have created the following book entity:

Book entity

Step 4: Creating the REST endpoints

To facilitate the REST endpoints, we have created the following REST controller. It serves the aforementioned endpoints:

BookController.java

We also have created the following repository:

BookRepository

Step 5: Insert data at application startup

To help with the testing, let's insert a few book details in our database. To do so, create a new SQL file named data.sql in src/main/resources directory. Spring boot automatically executes this file at startup.

data.sql

Step 6: Testing the API

Let us now start the application and test a few endpoints to ensure the application is working as expected. We are using HTTPPie to access the endpoint and we are receiving data from the server as shown below:
['Somnath Musib']
['Technology', 'Software Engineering', 'Coding', 'Software Development', 'Programming']