A robust Spring Boot REST API for managing books in a library system. This microservice provides comprehensive book management functionality with advanced filtering, pagination, and statistics capabilities.
- π Complete CRUD Operations - Create, read, update, and delete books
- π Advanced Search & Filtering - Search by title, author, genre, publication year, and availability
- π Pagination & Sorting - Efficient data retrieval with customizable page sizes and sorting options
- π Statistics Dashboard - Get insights into your book collection
- β Data Validation - Comprehensive input validation with meaningful error messages
- ποΈ Clean Architecture - Well-structured service layers following best practices
- π API Documentation - Swagger/OpenAPI integration for easy API exploration
- π Transaction Management - Reliable data consistency with Spring transactions
- Java 17+
- Spring Boot 3.x
- Spring Data JPA
- Spring Web
- Spring Validation
- Lombok - Reducing boilerplate code
- Swagger/OpenAPI 3 - API documentation
- MySQL/PostgreSQL - Database support
- Maven - Dependency management
- Java 17 or higher
- Maven 3.6+
- Database (MySQL/PostgreSQL for production)
-
Clone the repository
git clone https://github.com/yourusername/library-management-books.git cd library-management-books -
Configure the database
Update
application.propertiesorapplication.yml:# For Database # spring.datasource.url=jdbc:mysql://localhost:3306/library_db # spring.datasource.username=your_username # spring.datasource.password=your_password
-
Run the application
mvn spring-boot:run
-
Access the API
- API Base URL:
http://localhost:8080/api/books - Swagger UI:
http://localhost:8080/swagger-ui.html
- API Base URL:
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/books |
Create a new book |
GET |
/api/books |
Get all books with filtering & pagination |
GET |
/api/books/{id} |
Get book by ID |
PUT |
/api/books/{id} |
Update an existing book |
DELETE |
/api/books/{id} |
Delete a book |
GET |
/api/books/statistics |
Get book collection statistics |
POST /api/books
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Classic Literature",
"publishedYear": 1925,
"availableCopies": 5
}GET /api/books?author=Fitzgerald&availableOnly=true&page=0&size=10&sortBy=title&sortDirection=ascPUT /api/books/1
{
"title": "The Great Gatsby - Updated Edition",
"availableCopies": 3
}The API supports comprehensive filtering options:
title- Filter by book title (case-insensitive partial match)author- Filter by author name (case-insensitive partial match)genre- Filter by genre (case-insensitive partial match)yearFrom- Filter books published from this yearyearTo- Filter books published up to this yearavailableOnly- Show only books with available copiessearch- Global search across title, author, and genre
page- Page number (0-based, default: 0)size- Page size (default: 10)sortBy- Sort field (id, title, author, genre, publishedYear, availableCopies, createdAt, updatedAt)sortDirection- Sort direction (asc/desc, default: asc)
All API responses follow a consistent structure:
{
"success": true,
"message": "Books retrieved successfully",
"data": {
"content": [...],
"pageInfo": {
"page": 0,
"size": 10,
"first": true,
"last": false,
"hasNext": true,
"hasPrevious": false
},
"totalElements": 25,
"totalPages": 3
},
"timestamp": "2024-01-15 10:30:45",
"path": "/api/books"
}src/main/java/librarymanagement/books/
βββ BooksApplication.java
βββ controller/
β βββ BookController.java
βββ dto/
β βββ ApiResponse.java
β βββ BookFilterRequest.java
β βββ BookStatisticsResponse.java
β βββ BookUpdateRequest.java
β βββ PagedResponse.java
βββ exception/
β βββ GlobalExceptionHandler.java
βββ model/
β βββ Book.java
βββ repository/
β βββ BookRepository.java
βββ servicelayer/
βββ BookCreateService.java
βββ BookDefaultsHelper.java
βββ BookMapper.java
βββ BookQueryService.java
βββ BookUpdateService.java
| Field | Type | Constraints | Description |
|---|---|---|---|
id |
Long | Auto-generated | Unique identifier |
title |
String | Required, 1-200 chars | Book title |
author |
String | Required, 1-100 chars | Author name |
genre |
String | Optional, max 255 chars | Book genre |
publishedYear |
Integer | 1000-2026 | Publication year |
availableCopies |
Integer | Min 0 | Available copies count |
createdAt |
LocalDateTime | Auto-generated | Creation timestamp |
updatedAt |
LocalDateTime | Auto-updated | Last update timestamp |
Key configuration options in application.properties:
# Server configuration
server.port=8080
# Database configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# Swagger configuration
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
# Logging
logging.level.librarymanagement.books=DEBUGOnce the application is running, you can explore the full API documentation at:
- Swagger UI:
http://localhost:8080/swagger-ui.html - OpenAPI JSON:
http://localhost:8080/api-docs
This project is licensed under the MIT License - see the LICENSE file for details.