This is the backend API for the Library Management System built using Spring Boot.
It provides authentication, role-based authorization, and full library management features including book borrowing with business rules.
- Java 8
- Spring Boot
- Spring Security (JWT Authentication)
- Spring Data JPA (Hibernate)
- MySQL
- Maven
- Lombok
com.example.LibraryManagementSystem
│
├── controller # REST Controllers (API endpoints)
├── service # Business Logic layer
├── repository # JPA Repositories (Database access)
├── entity # Database Entities
├── dto # Data Transfer Objects
├── config # Application & general configurations
├── security # JWT, filters, authentication, authorization
├── exception # Custom exceptions & global exception handling
├── enums # Application enums (Role, Status, etc.)
The system uses Spring Security with JWT.
ADMINUSER
- ADMIN → Full access (manage books, authors, users)
- USER → Can borrow and return books
- Borrowing endpoints are protected with:
@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
In this system:
User acts as the library member.
There is no separate Member borrowing logic.
All borrow operations are linked to the authenticated User.
- Create Author
- Update Author
- Delete Author
- Get All Authors
- Add Book
- Update Book
- Delete Book
- Get All Books
- Track available copies
Business Rules:
- A user can borrow maximum 5 books at a time.
- Each borrowed book must be returned within 7 days.
- A book cannot be borrowed if
availableCopies = 0. - Same copy cannot be borrowed by multiple users simultaneously.
Endpoint
POST /api/borrow/{userId}/{bookId}
Process
- Check user exists
- Check book exists
- Check user has less than 5 active borrowings
- Check book availability
- Create borrowing record
- Set due date = borrowDate + 7 days
- Decrease availableCopies
Endpoint
PUT /api/borrow/return/{borrowingId}
Process
- Check borrowing record exists
- Ensure book is not already returned
- Mark as returned
- Increase availableCopies
- id
- username
- password
- role
- id
- name
- id
- title
- totalCopies
- availableCopies
- author
- id
- borrowDate
- dueDate
- returned
- user (ManyToOne)
- book (ManyToOne)
countByUserAndReturnedFalse(user)ensures max 5 books- Due date automatically set using:
LocalDate.now().plusDays(7);
git clone <your-repo-url>
Update application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/library_db
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
mvn spring-boot:run
Server runs on:
http://localhost:8080
- Login and get JWT token.
- Add token to Authorization header:
Authorization: Bearer <your_token>
- Test borrow and return endpoints.
- Late return penalty system
- Borrow history endpoint
- Pagination & filtering
- Exception handling with @ControllerAdvice
- Swagger documentation
This project is for educational purposes.
Developed as a Spring Boot practice project.