1.Building the catalog service
- In first chapter of this series, we’ll start with a web application that exposes its functionality over HTTP through a REST API using Spring Boot. NovelNest system has a Catalog Service application that’s responsible for managing the books available in the catalog.
Initializing the project
- We’ll generate a Spring project from IDEA and store it in the
catalog-service
repository onGithub
(which belongs toNovelNest
organization)
REST API Specifications
- Catalog Service will be responsible for supporting the following use cases:
- View the list of books in the catalog
- Search books by their ISBN.
- Add a new book to the catalog
- Edit information for an existing book.
- Remove a book from the catalog.
In other words, we can say the application should provide an API to perform CRUD operations on books.
Endpoint | HTTP Method | Request body | Status | Response body | Description |
---|---|---|---|---|---|
/v1/books | GET | 200 | Book[] | Get all the books in the catalog | |
/v1/books | POST | Book | 201 | Book | Add a new book to the catalog |
402 | A book with the same ISBN already exists | ||||
/v1/books/{isbn} | GET | 200 | Book | Get the book with the given ISBN | |
404 | No book with the given ISBN exists | ||||
/v1/books/{isbn} | PUT | Book | 200 | Book | Update the book with the given ISBN |
201 | Book | Create a book with the given ISBN | |||
/v1/books/{isbn} | DELETE | 204 | Delete the book with the given ISBN |
Business logic
The contract is established through the REST API, so let’s move on and look at the business logic. The solution is centered around three concepts:
Entity
: an entity represents the noun in a domain. In this case: “Book”.Service
: a service defines the use cases for the domain. For example: “adding a book to the catalog”.Repository
: a repository is an abstraction to let the domain layer access data independently from its source.
Defining the domain entity
- In the catalog service project, create a new
com.novelnest.catalogservice.domain
package for the business logic, create aBook
java record to represent the domain entity.