Welcome to my Spring Boot app!
This project was developed to demonstrate how to build an API using Spring Boot. It has table relationships in the database and comprehensive unit and integration testing.
- β Java 17
- β SpringBoot
- β Spring MVC
- β Spring Data JPA
- β SpringSecurity
- β PostgreSQL
- β JWT
- β Docker
- β JUnit 5 + Mockito(unit tests) and MockMvc(integration tests)
- SOLID
- Containerization with Docker
- Dependency Injection
- Queries with SpringData JPA
- Error Handling
- Clone the repository:
$ git clone https://github.com/Guilhermebit/spring-boot-rest-api.git
-
Install dependencies with Maven
-
Install docker: https://docs.docker.com/engine/install/
-
Run the following command:
./mvnw clean install
- Run the following command:
docker-compose up
- The API will be accessible at http://localhost:8081
This project includes a Python script that automates the application build process.
Here you can download Python: https://www.python.org/downloads/
To run the script, navigate to the project directory and execute:
- On Windows:
py build.py
- On Unix-based systems:
python build.py
The script performs the following steps:
-
Compiling an application: Use Maven Wrapper to compile an application with the correct command for the operating system:
- Windows:
mvnw.cmd clean package
- Unix systems (Linux/Mac):
./mvnw clean package
- Windows:
-
Stop and remove all Docker containers: Run
docker-compose down
to ensure all containers are stopped and removed. -
Building and starting Docker containers: Use
docker-compose up --build -d
to build Docker images and start containers in the background.
The script was designed to simplify the process of building, stopping and restarting containers, saving time and making the process faster.
To test the HTTP requests below, the Postman tool was used.
If you choose to use Postman
, you can download the Collection
by clicking here, and import it into your Postman.
Here you can download Postman: https://www.postman.com/downloads/
- The user must have the
TOKEN
and anADMIN
role to access the routes:POST /product
PUT /product/{id}
DELETE /product/{id}
POST /auth/register
-
Request (application/json)
-
Body
{ "login": "User1", "password": 1234, "role": "USER" }
-
-
Response 201 (application/json)
-
Body
{ "data": "null", "message": "Your registration was successful", "status": 201 }
-
POST /auth/login
-
Request (application/json)
-
Body
{ "login": "User1", "password": 1234, }
-
-
Response 200 (application/json)
-
Body
{ "data": [ { "token": "access_token", } ], "message": "access_token", "status": 200 }
-
POST /product
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
Body
{ "name": "t-shirt", "price_in_cents": 5000 }
-
-
Response 201 (application/json)
-
Body
{ "data": [ { "id": "c2fc6ab7-cdf1-46ee-be87-804df6be6731", "name": "t-shirt", "price_in_cents": 5000 } ], "message": "", "status": 201 }
-
GET /product
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
-
Response 200 (application/json)
-
Body
{ "data": [ { "id": "c2fc6ab7-cdf1-46ee-be87-804df6be6731", "name": "t-shirt", "price_in_cents": 5000 } ], "message": "", "status": 200 }
-
GET /product/{id}
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
-
Response 200 (application/json)
-
Body
{ "data": [ { "id": "c2fc6ab7-cdf1-46ee-be87-804df6be6731", "name": "t-shirt", "price_in_cents": 5000 } ], "message": "", "status": 200 }
-
GET /product/value/{3000}/{5000}
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
-
Response 200 (application/json)
-
Body
{ "data": [ { "id": "c2fc6ab7-cdf1-46ee-be87-804df6be6731", "name": "t-shirt", "price_in_cents": 5000 } ], "message": "", "status": 200 }
-
PUT /product/{id}
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
Body
{ "name": "t-shirt blue", "price_in_cents": 3000 }
-
-
Response 200 (application/json)
-
Body
{ "data": [ { "id": "c2fc6ab7-cdf1-46ee-be87-804df6be6731", "name": "t-shirt blue", "price_in_cents": 3000 } ], "message": "", "status": 200 }
-
DELETE /product/{id}
- OBS: The data is not deleted directly, instead its status is changed to "false"
-
Request (application/json)
-
Headers
Authorization: Bearer [access_token]
-
-
Response 204 (application/json)
-
Body
{ "data": null, "message": "Product successfully deleted.", "status": 204 }
-