Projects › Intermediate
Library Management REST API
A production-style REST API to manage books, members, issues and returns with fines — the go-to Spring Boot portfolio project.
Level: Intermediate · Time: 1–2 weeks · Tech stack: Spring Boot, Spring Data JPA, MySQL, Spring Security
Features to build
- CRUD for books and members
- Issue / return with due dates and late fines
- Search and pagination
- JWT login with ADMIN and MEMBER roles
- Validation, global error handling and Swagger docs
What you will learn
- Layered architecture (controller/service/repository)
- JPA relationships
- Transactions
- Spring Security + JWT
- Testing with MockMvc
Step-by-step guide
1. Start
Spring Initializr with Web, Data JPA, Validation, MySQL Driver, Security, Lombok.
2. Entities
Book (isbn, title, author, copies), Member, Loan (book, member, issuedOn, dueOn, returnedOn, fine) with @ManyToOne relations.
3. Issue a book safely
@Transactional
public Loan issue(Long bookId, Long memberId) {
Book b = books.findById(bookId).orElseThrow(() -> new NotFoundException("Book not found"));
if (b.getCopies() == 0) throw new BadRequestException("No copies left");
if (loans.countByMemberIdAndReturnedOnIsNull(memberId) >= 3) throw new BadRequestException("Limit reached");
b.setCopies(b.getCopies() - 1);
return loans.save(new Loan(b, members.getReferenceById(memberId), LocalDate.now(), LocalDate.now().plusDays(14)));
}
@Transactional keeps the copy count and the loan consistent.
4. Fines
On return: fine = max(0, days late) × ₹5, using ChronoUnit.DAYS.between(dueOn, today).
5. DTOs + validation + errors
Request records with @NotBlank/@Min, and a @RestControllerAdvice returning clean JSON errors.
6. Security
JWT filter; /api/books/** GET public, write operations hasRole('ADMIN').
7. Docs & tests
springdoc-openapi for Swagger UI; @WebMvcTest for controllers, @DataJpaTest for repositories.
Level it up
- Email reminders for due dates (@Scheduled)
- Docker + deploy on Render
- Reservations queue
On your resume
- Developed a Spring Boot REST API for library management with JPA, transactional issue/return, fine calculation, JWT role-based security, Swagger docs and MockMvc tests.