Skip to content

πŸ–₯️ REST API developed in Spring Boot, including tests for the back-end

License

Notifications You must be signed in to change notification settings

Guilhermebit/spring-boot-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spring Boot API

Docker Pulls Java Build and Publish a Docker Image licence mit

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.

This project has the following architecture:

spring_boot_api_diagram

Tecnologies

  • βœ… Java 17
  • βœ… SpringBoot
  • βœ… Spring MVC
  • βœ… Spring Data JPA
  • βœ… SpringSecurity
  • βœ… PostgreSQL
  • βœ… JWT
  • βœ… Docker
  • βœ… JUnit 5 + Mockito(unit tests) and MockMvc(integration tests)

Practices Adopted

  • SOLID
  • Containerization with Docker
  • Dependency Injection
  • Queries with SpringData JPA
  • Error Handling

πŸš€ Installation

  1. Clone the repository:
$ git clone https://github.com/Guilhermebit/spring-boot-rest-api.git
  1. Install dependencies with Maven

  2. Install docker: https://docs.docker.com/engine/install/

  3. Run the following command:

./mvnw clean install

Usage

  1. Run the following command:
docker-compose up 
  1. The API will be accessible at http://localhost:8081

βš™οΈ Build Script

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
  • 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.

Api EndPoints

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 an ADMIN role to access the routes:
    • POST /product
    • PUT /product/{id}
    • DELETE /product/{id}

Register a new user

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
      }

Login

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
      }

Insert a new product

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 all products

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 one product

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 the product between a range of values

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
      }

Update a product

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 a product

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
      }