A RESTful API for managing books and authors, built with Java and Spring Boot. The API provides CRUD operations for books and authors and manages the relationship between them.
- Java
- Spring Boot
- Spring Web
- Spring Data JPA
- Hibernate
- PostgreSQL
- Maven
- Lombok
- Bean Validation
- Author CRUD
- Book CRUD
- Author–Book relationship
- Request validation
- Partial updates with
PATCH - Global exception handling
- Business rule validation
- PostgreSQL persistence
| Method | Endpoint | Description |
|---|---|---|
| GET | /authors |
Get all authors |
| GET | /authors/{id} |
Get an author |
| POST | /authors |
Create an author |
| PATCH | /authors/{id} |
Update an author |
| DELETE | /authors/{id} |
Delete an author |
| Method | Endpoint | Description |
|---|---|---|
| GET | /books |
Get all books |
| GET | /books/{isbn} |
Get a book |
| POST | /books |
Create a book |
| PATCH | /books/{isbn} |
Update a book |
| DELETE | /books/{isbn} |
Delete a book |
The project follows a layered architecture:
src/
├── main/
│ ├── java/
│ │ └── com.marouane.library/
│ │ ├── controller/
│ │ ├── service/
│ │ ├── repository/
│ │ ├── entity/
│ │ ├── dto/
│ │ ├── command/
│ │ ├── mapper/
│ │ ├── exception/
│ │ └── projections/
│ │
│ └── resources/
│ └── application.properties
│
└── test/
- Controller : Handles HTTP requests and responses.
- Mapper : Converts between DTOs, Commands, and Entities.
- DTO / Command : Transfers data between the API and application layers.
- Service : Contains business logic and business rules.
- Repository : Handles database access using Spring Data JPA.
- Entity : Represents the database model.
- Exception : Contains application exceptions and global exception handling.
The application uses PostgreSQL.
Create the PostgreSQL database:
CREATE DATABASE library_db;Create a .env file in the project root:
DB_URL=jdbc:postgresql://localhost:5432/library_db
DB_USERNAME=postgres
DB_PASSWORD=your_passwordReplace the
database usernameandpasswordwith your own PostgreSQL credentials.
- Java 21
- PostgreSQL
- Maven
git clone https://github.com/marouaneaddou/Book-Library-API.git
cd Book-Library-APIMake sure
PostgreSQLis running and thedatabase has been created.`
Configure your
environmentvariables with thePostgreSQL connection details.
Using Maven Wrapper:
./mvnw spring-boot:runOn Windows:
mvnw.cmd spring-boot:runOr using Maven:
mvn spring-boot:runThe API will be available at:
http://localhost:8080
Possible future improvements:
- Authentication
- Authorization and role-based access control
- Pagination
- Filtering and sorting
- Book search
- OpenAPI / Swagger documentation
- Docker support
This project was built to practice Java backend development with Spring Boot and apply REST API design, layered architecture, database persistence, validation, and business logic separation in a practical project.