This project is a First REST API application created as part of a Java programming course at Vistula University. It is built using the Spring Boot Framework to demonstrate the implementation of a RESTful API for managing products. The application supports CRUD (Create, Read, Update, Delete) operations and integrates with Spring Data JPA and Hibernate for database interactions. Additionally, H2 Database is used for in-memory data persistence, and Swagger UI is included for API documentation and testing.
The application provides the following functionalities:
- Create a Product (HTTP POST)
- Retrieve a Product by ID (HTTP GET)
- Retrieve All Products (HTTP GET)
- Update a Product (HTTP PUT)
- Delete a Product (HTTP DELETE)
- Exception Handling for invalid requests or missing resources.
- Java 21: Main programming language.
- Spring Boot 3.1.4: Framework for developing the REST API.
- Spring Data JPA: For seamless integration with the database.
- Hibernate: ORM framework for database operations.
- H2 Database: In-memory database for data storage.
- Swagger UI: API documentation and testing.
- Maven: Dependency and build management.
The project follows a layered architecture:
- api: Contains request and response classes for managing API requests and responses.
- domain: Represents the entity class (
Product) with annotations for Hibernate. - repository: Interfaces for data access, including integration with
JpaRepository. - service: Business logic for handling product operations.
- support: Utility classes and exception handling mechanisms.
- shared.api.response: Classes for consistent error response formatting.
- resources: Configuration files, including application properties and Swagger setup.
Ensure the following are installed on your system:
- Java 21
- Maven
-
Clone the repository:
git clone <repository-url> cd FirstRestApi
-
Build and run the application:
mvn spring-boot:run
-
Access the application:
- API base URL:
http://localhost:8080 - Swagger UI:
http://localhost:8080/swagger-ui/index.html
- API base URL:
The API can be tested using:
- Swagger UI: Navigate to the provided URL to test API endpoints interactively.
- Postman: Import the API endpoints and test requests manually.
To view the H2 Database Console:
- Go to
http://localhost:8080/console/ - Use the following credentials:
- JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password: (leave blank)
- JDBC URL:
The application includes a custom exception handling mechanism:
- ProductNotFoundException: Thrown when a product with a specific ID is not found.
- Global Exception Handler: Formats exceptions into a readable
ErrorMessageResponseJSON structure.
- Implement authentication and authorization.
- Add more comprehensive logging using SLF4J.
- Enhance data validation for product inputs.
- Sebastian Ciborowski - Using materials provided by lecturer Dominik Bielecki of Vistula University
This project is open-source and does not have any specific license, meaning it can be freely used and modified for educational or personal purposes.