Skip to content

Throyer/springboot-api-rest-example

Repository files navigation

🚨 I'm working on a migration to spring boot 3, it's not completely done yet, but the branch is this: https://github.com/Throyer/springboot-api-rest-example/tree/spring-boot-3-migration the focus is on making the experience with docker better, 100% test coverage, the rate-limit is in an nginx container and most of the configurations I tried to simplify

🇧🇷 In Portuguese

🐬 MySQL/MariaDB (outdated) implementation

Tecnologias

Spring Boot API RESTful

A complete user registry, with access permissions, JWT token, integration and unit tests, using the RESTful API pattern.



Live demo on heroku

Demonstration

Table of Contents

Features

Tecnologias

Requirements

  • Postgres: ^13
  • Java: ^17
  • Maven: ^3.8.4

This project was started with Spring Initializr.

Entities

database diagram

🚨 draw.io file here

Docker examples

🚨 create environment file and add permission to execute scripts

cp .docker/.env.example .docker/.env && chmod -R +x .docker/scripts
  • docker-compose for development

    • starting containers
    .docker/scripts/develop up -d --build 
    • removing contaiers
    .docker/scripts/develop down 
    • show backend logs
    .docker/scripts/develop logs -f api 
  • docker-compose for production

    .docker/scripts/production up -d --build 
    .docker/scripts/production down 

Local Installation

🚨 check requirements or if you are using docker check docker development instructions

  • clone the repository and access the directory.
    git clone [email protected]:Throyer/springboot-api-crud.git crud &&cd crud
  • download dependencies
    mvn -f api/pom.xml install -DskipTests
  • run the application (available at: localhost:8080)
    mvn -f api/pom.xml spring-boot:run
  • running the tests
    mvn -f api/pom.xml test
  • to build for production
    mvn -f api/pom.xml clean package
  • to generate the coverage report after testing (available at: api/target/site/jacoco/index.html)
    mvn -f api/pom.xml jacoco:report

Tests

Coverage Status

Running a specific test

use the parameter -Dtest=<class>#<method>

  • for example the integration test. creating a user:
    mvn -f api/pom.xml test -Dtest=UsersControllerTests#should_save_a_new_user

Swagger

Once the application is up, it is available at: localhost:8080/docs

🚨 if you set SWAGGER_USERNAME and SWAGGER_PASSWORD on application.properties file this route require authentication

example on heroku


Database Migrations

Creating database migration files

🚨 check requirements

if you using docker-compose

.docker/scripts/mvn migration:generate -Dname=my-migration-name 
  • Java based migrations

    mvn -f api/pom.xml migration:generate -Dname=my-migration-name
  • SQL based migrations

    mvn -f api/pom.xml migration:generate -Dname=my-migration-name -Dsql

Environment variables

DescriptionParameterDefault values
server portSERVER_PORT8080
database hostDB_HOSTlocalhost
database portDB_PORT5432
database nameDB_NAMEexample
database usernameDB_USERNAMEroot
database user passwordDB_PASSWORDroot
displays the generated sql in the loggerDB_SHOW_SQLfalse
set maximum database connectionsDB_MAX_CONNECTIONS5
secret value in token generationTOKEN_SECRETsecret
secret hash idsHASHID_SECRETsecret
token expiration time in hoursTOKEN_EXPIRATION_IN_HOURS24
refresh token expiry time in daysREFRESH_TOKEN_EXPIRATION_IN_DAYS7
SMTP server addressSMTP_HOSTsmtp.gmail.com
SMTP server portSMTP_PORT587
SMTP usernameSMTP_USERNAMEuser
SMTP server passwordSMTP_PASSWORDsecret
time for recovery email to expireMINUTES_TO_EXPIRE_RECOVERY_CODE20
max requests per minuteMAX_REQUESTS_PER_MINUTE50
swagger urlSWAGGER_URL/docs
swagger usernameSWAGGER_USERNAMEnull
swagger passwordSWAGGER_PASSWORDnull

these variables are defined in: application.properties

# to change the value of some environment variable at runtime# on execution, just pass it as a parameter. (like --SERVER_PORT=80). $ java -jar api-5.0.0.jar --SERVER_PORT=80

All options of aplication.properties here.

All features of Spring Boot.

Star History

Star History Chart