Skip to content

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 on Github (which belongs to NovelNest organization)

NovelNest Architecture

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.

EndpointHTTP MethodRequest bodyStatusResponse bodyDescription
/v1/booksGET200Book[]Get all the books in the catalog
/v1/booksPOSTBook201BookAdd a new book to the catalog
402A book with the same ISBN already exists
/v1/books/{isbn}GET200BookGet the book with the given ISBN
404No book with the given ISBN exists
/v1/books/{isbn}PUTBook200BookUpdate the book with the given ISBN
201BookCreate a book with the given ISBN
/v1/books/{isbn}DELETE204Delete 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 a Book java record to represent the domain entity.
package com.novelnest.catalogservice.domain;
public record Book (
String isbn,
String title,
String author,
Double price
){}

Tests

Unit tests

Integration tests