- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Build a Book Catalog API with Spring Boot 4
This hands-on lab is designed to help you complete a book catalog application with Spring Boot 4. A series of guided steps will give you a walkthrough on how to implement some of the core layers that compose a Spring Boot application such as the services in a business layer along with the controller and views of the presentation layer. By the end of the lab, you will have demonstrated your ability to build a fully functioning book catalog capable of adding, editing, and removing books within its inventory.
Lab Info
Table of Contents
-
Challenge
Introduction
The purpose of this lab is to guide you through the basics of building an application in Spring Boot 4. This lab will cover the views and controller of the presentation layer, as well as, the service used in the business layer. Since you will be working with a local JSON file for data, the persistence and database layers will be outside the scope of this lab.
You can run the application after completing the lab by clicking the Run button in the Terminal or by running the command
./gradlew bootRun. Once your application is running, visit your application using this URL: {{localhost:8081}}On the Please sign in screen, type
adminfor the Username andpspassword123for the Password. -
Challenge
Implementing the Service
The first component of this application you will need to complete is the
BookCatalogService.java. This service contains all of the logic and functions your application will need to perform such as adding, editing, and deleting books.The boilerplate code to the service already instantiates the catalog as a list of book data from a json file and sorts it by
idfor you. Next, you will need to implement thebinarySearch()function to search for the index of a book in the book catalogue using the binary search algorithm. Next, you will need to implement theaddBook()method, which should generate a newBookbased on the given parameters before adding it to the catalog of books. Now, you will to implement theupdateBook()method, which will update the specified book in the book catalog with its updated values. Lastly, you will need to implement theremoveBook()method, which will use thebinarySearch()function you implemented earlier to remove a book from the catalog. -
Challenge
Implementing the Controller
Now you need to complete the controller for the application, which is located in
BookController.java. The controller is responsible for handling HTTP requests from the apps HTML views by using the methods implemented inBookCatalogService. It then passes back data to the views to be displayed for the user.The first endpoint you will need to implement is the
homePage(). Next, you will need to implement theeditBookPage()endpoint for when the user wishes to edit a book currently in the catalog. TheupdateBook()endpoint is then called after the user has inputted any edits to the book they wish to modify and submits those changes. TheaddBookPage()is called when the user wishes to add a new book to the catalog. Like theupdateBook()endpoint, theaddNewBook()endpoint is called after the user has inputted the data for a new book to be added and submits them. Lastly, thedeleteBook()endpoint is called when a user wishes to delete a book from the catalog. -
Challenge
Implementing the View
Lastly, you will need to add some finishing touches to the views layer. In this application, the Thymeleaf template engine from Spring is used to render HTML files such as
home.html,add-book.html, andedit-book.html. The controller endpoints you implemented in the previous step dictates which of these HTML files the user is currently viewing at any given time.While the HTML files are mostly complete, you will need to add a few Thymeleaf specific attributes to certain elements that will dynamically render data from the catalog. Let's start with
home.html. Next, you will need to add the appropriate attribute bindings toadd-book.html. Once more, you will also need to add the appropriate attribute bindings toedit-book.html. With all of these tasks completed, the application is now complete and should be fully functional! Refer back to the introduction on running and viewing the application.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.