This document provides a detailed explanation of the different components of the Library Borrowing System built with Spring Boot.
The project follows the MVC (Model-View-Controller) pattern. Notices that the skeleton of the project can be generated with Sprint initializer.
In summary, the layers that are involved are:
- Controller: Handles HTTP requests and sends responses.
- Service: Contains business logic and interacts with repositories.
- Repository: Manages data access and CRUD operations using Spring Data JPA.
- Entity: Represents the data model and maps to a database table.
- Located in
src/main/java/com/example/library/model/ - Defines JPA entities representing database tables.
- Uses Hibernate annotations for ORM mapping.
- Example:
@Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String author; }
- Spring Data JPA Documentation
- Located in
src/main/java/com/example/library/repository/ - Interfaces extend JpaRepository to provide CRUD operations.
- Example:
public interface BookRepository extends JpaRepository<Book, Long> {}
- Spring Data Repositories
- Located in
src/main/java/com/example/library/service/ - Contains business logic.
- Example:
@Service public class BookService { private final BookRepository bookRepository; public List<Book> getAllBooks() { return bookRepository.findAll(); } }
- Spring Service Layer
- Located in
src/main/java/com/example/library/controller/ - Handles HTTP requests and routes them to services.
- Example:
@RestController @RequestMapping("/books") public class BookController { private final BookService bookService; @GetMapping public List<Book> getBooks() { return bookService.getAllBooks(); } }
- Spring MVC Controllers
- Located in
src/main/resources/static/ - Contains HTML, JavaScript, and CSS for the UI.
- It could use Thymeleaf as the templating engine. However, it uses simple HTML with JavaScript code.
- Example:
<html> <body> <h1>Library System</h1> <script src="app.js"></script> </body> </html>
- Thymeleaf Documentation
- Uses Springdoc OpenAPI for generating API documentation.
- URL:
http://localhost:8080/swagger-ui.html - Example Configuration:
@OpenAPIDefinition(info = @Info(title = "Library API", version = "1.0")) @Configuration public class OpenAPIConfig {}
- Springdoc OpenAPI
When building a REST API in Java using Spring Boot, it’s common to use Data Transfer Objects (DTOs) to encapsulate data that will be sent or received by the API. DTOs are simple objects that only contain fields and getter/setter methods, without any business logic, and are used to transfer data between layers of the application.
- Example DTO:
package com.example.demo.dto; public class ProductDTO { private Long id; private String name; private String description; private double price; // Constructors, getters, and setters public ProductDTO() {} public ProductDTO(Long id, String name, String description, double price) { this.id = id; this.name = name; this.description = description; this.price = price; } // Getters and Setters }
- Start MySQL and create the database:
mysql -u root -p < src/main/resources/dbsetup.sql - Update
application.propertieswith database credentials:spring.datasource.url=jdbc:mysql://localhost:3306/libraryapidb spring.datasource.username=root spring.datasource.password=password
- Start the application:
mvn clean compile mvn spring-boot:run
-
Run all tests:
mvn test -
Run unit tests only:
mvn -Dtest=UserServiceTest,BookServiceTest test mvn -Dtest=*ServiceTest test
-
Run integration tests:
mvn verify mvn -Dtest=LibraryIntegrationTest test -
Run performance tests:
mvn -Dtest=PerformanceTest test -
Run tests providing more details:
mvn verify mvn test -Dspring-boot.run.profiles=test -
Generate a test report (JaCoCo, PMD, Checkstyle):
mvn site
- Spring Boot Reference Guide
- Spring Framework Documentation
- Spring Boot Guides
- Very good explaination of the project at Medium, only including Book service
- Building REST services with Sprint
- Docker example with Spring
- Documentation about JaCoCo and Spring Boot
- Documentation JUnitPerf
- JaCoCo official documentation