This is a Spring Boot-based Library Management System API that allows librarians to manage books, patrons, and borrowing records. The API supports CRUD operations for books and patrons, as well as borrowing and returning books. It also includes JWT-based authentication for secure access to protected endpoints.
- Prerequisites
- Running the Application
- API Endpoints
- Authentication
- Using Swagger
- Testing
- Caching
- Contributing
Before running the application, ensure you have the following installed:
- Java 17 or later
- Maven or Gradle
- MySQL or any other compatible database
- Postman or any API testing tool (optional, if not using Swagger)
- Clone the Repository:
git clone https://github.com/kasemsa/Library-Management-System-using-Spring-Boot.git cd library-management-system
- Configure the Database:
Update the application.properties file with your database credentials:
spring.datasource.url=jdbc:mysql://localhost:3306/library_management
spring.jpa.hibernate.ddl-auto=update
- Build the Application:
Using Maven:
mvn clean install
Using Gradle:
./gradlew build
- Run the Application: Using Maven:
mvn spring-boot:run
Using Gradle:
./gradlew bootRun
- Access the API: The API will be available at http://localhost:8080.
API Endpoints
Book Management:
GET /api/books: Retrieve a list of all books.
GET /api/books/{id}: Retrieve details of a specific book by ID.
POST /api/books: Add a new book to the library.
PUT /api/books/{id}: Update an existing book's information.
DELETE /api/books/{id}: Remove a book from the library.
Patron Management:
GET /api/patrons: Retrieve a list of all patrons.
GET /api/patrons/{id}: Retrieve details of a specific patron by ID.
POST /api/patrons: Add a new patron to the system.
Borrowing Management:
POST /api/borrow/{bookId}/patron/{patronId}: Allow a patron to borrow a book.
PUT /api/return/{bookId}/patron/{patronId}: Record the return of a borrowed book by a patron.
Authentication The API uses JWT (JSON Web Token) for authentication. To access protected endpoints, follow these steps:
- Register a User:
Send a POST request to /api/users with the following payload:
{
"username": "admin",
"email": "admin@example.com",
"password": "password123"
}
- Login:
Send a POST request to /auth/login with the following payload:
{
"username": "admin",
"password": "password123"
}
.The response will include a JWT token:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
-
Use the Token:
Include the token in the Authorization header for protected endpoints:
Authorization: Bearer <your-jwt-token>
Using Swagger
The API documentation is available via Swagger UI. Follow these steps to explore and test the API:
-
Start the application (see Running the Application).
-
Open your browser and navigate to:
http://localhost:8080/swagger-ui.html
-
Use the Swagger UI to:
.View detailed documentation for each endpoint.
.Test endpoints directly from the UI.
.View example requests and responses.
Testing
To run the tests, use the following commands:
Maven:
mvn test
Gradle:
./gradlew test
Caching
The API uses Spring's caching mechanisms to improve performance. Frequently accessed data, such as book details and patron information, is cached.
Cache Configuration:
Cache size: 100 entries
Cache expiration: 60 seconds after last access
Contributing
Contributions are welcome! Please follow these steps:
-
Fork the repository.
-
Create a new branch for your feature or bugfix.
-
Submit a pull request with a detailed description of your changes.