I had the fortune of growing up in a house full of books, and several times while growing up, I wanted to organize and arrange them in some way to make it easier to find them and know what books were there. After finishing university, I tried to do it with a couple of applications, but none of them fully convinced me. So, as soon as I started learning programming, I had the idea in my mind to create my own application.

And it all begins here:

Book Management System API on Java and SpringBoot

Logo de Github (sólo el contorno del gato)

The code provided is a basic Spring Boot application with RESTful API functionality to manage a home library and includes both integration and unit testing.

Here’s what the code does summarized:

  • Libro.java (The Book Details): it describes each book. For every book, you can note down its name, author, and where it is in the library.
  • LibroRepository.java (The Bookkeeper): This part keeps track of all your books. It knows how to save, find, and delete books.
  • LibroController.java (The Book Manager): It helps you manage your books. It understands commands like “Show me all the books” or “Add a new book.”
  • BibliotecaApplication.java (The Starter): This starts the whole library system. It’s where the adventure begins.
  • Tests (The Checkers): There are little checkers to make sure everything is working correctly. They’re like friends who make sure the tool does what it’s supposed to do.

Project Structure

The project is structured as a Spring Boot application.

  • The main code is located in the src/main/java/net/Karvala/Biblioteca directory.
  • The code includes a RESTful controller (LibroController), an entity class (Libro), a repository interface (LibroRepository), and the main application class (BibliotecaApplication).
  • Tests are provided in the src/test directory.

Libro Entity (Libro.java)

Represents a book with attributes such as title, author, and shelf.

  • Annotated with @Entity to indicate it is a JPA entity.
  • Uses @Id and @GeneratedValue annotations for primary key generation.
captura de pantalla del código
captura de pantalla del código

Libro Repository (LibroRepository.java)

Extends JpaRepository, providing CRUD operations for the Libro entity.

Libro Controller (LibroController.java)

Implements CRUD operations for managing books using Spring Web’s RESTful capabilities.

  • Uses dependency injection to inject a LibroRepository instance.
  • Defines methods for listing all books, finding a book by ID, creating a new book, updating a book, and deleting a book.
captura de pantalla de parte del código
captura de pantalla de parte del código

Biblioteca Application (BibliotecaApplication.java)

The main class that runs the Spring Boot application.

  • Demonstrates the usage of the LibroRepository by creating, saving, and retrieving books

Tests

  • LibroControllerTest.java performs integration tests for the LibroController.
  • LibroControllerUnitTest.java uses Mockito to perform unit tests for the LibroController.
captura de pantalla de parte del código

Code Highlights

  • Dependency Injection: Constructor injection is used in LibroController to inject the LibroRepository instance.
  • RESTful Endpoints: CRUD operations are implemented using Spring’s annotations (@GetMapping, @PostMapping, @PutMapping, @DeleteMapping).
  • Response Handling: Utilizes ResponseEntity for more control over HTTP responses.
  • Testing: Integration tests (LibroControllerTest.java) use TestRestTemplate to test the actual HTTP endpoints. Unit tests (LibroControllerUnitTest.java) use Mockito for mocking the repository and testing controller methods independently.

How It Works

  1. Entity and Repository:

    Libro class represents a book, and LibroRepository is an interface for CRUD operations on books.

  2. Controller (LibroController.java):
    • Handles HTTP requests related to books.
    • Uses LibroRepository for database operations.
    • Defines endpoints for finding all books, finding a book by id, creating, updating, and deleting books.
  3. Application (BibliotecaApplication.java): Initializes the Spring Boot application.
    • Injects the LibroRepository.
    • Performs sample CRUD operations on books.
  4. Tests:
    • LibroControllerTest.java: Integration tests using TestRestTemplate.
    • LibroControllerUnitTest.java: Unit tests using Mockito and MockMvc.
The application follows the standard Spring Boot structure and principles, using JPA for database operations and providing RESTful endpoints for book management. The tests ensure the correctness of the controller’s behavior in different scenarios.