Code Monkey home page Code Monkey logo

springboot-api-rest-example's Introduction

馃毃 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

Description Parameter Default values
server port SERVER_PORT 8080
database host DB_HOST localhost
database port DB_PORT 5432
database name DB_NAME example
database username DB_USERNAME root
database user password DB_PASSWORD root
displays the generated sql in the logger DB_SHOW_SQL false
set maximum database connections DB_MAX_CONNECTIONS 5
secret value in token generation TOKEN_SECRET secret
secret hash ids HASHID_SECRET secret
token expiration time in hours TOKEN_EXPIRATION_IN_HOURS 24
refresh token expiry time in days REFRESH_TOKEN_EXPIRATION_IN_DAYS 7
SMTP server address SMTP_HOST smtp.gmail.com
SMTP server port SMTP_PORT 587
SMTP username SMTP_USERNAME user
SMTP server password SMTP_PASSWORD secret
time for recovery email to expire MINUTES_TO_EXPIRE_RECOVERY_CODE 20
max requests per minute MAX_REQUESTS_PER_MINUTE 50
swagger url SWAGGER_URL /docs
swagger username SWAGGER_USERNAME null
swagger password SWAGGER_PASSWORD null

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

springboot-api-rest-example's People

Contributors

dependabot[bot] avatar throyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

springboot-api-rest-example's Issues

ERRO 500 Internal Server Error ao tentar obter um token.

Na vers茫o atual da aplica莽茫o, ai tentar obter um token atrav茅s do endpoint /api/sessions a aplica莽茫o est谩 apresentando o erro abaixo.

message=failed to lazily initialize a collection of role: com.github.throyer.common.springboot.api.domain.models.entity.User.roles, could not initialize proxy -
no Session, path=/api/sessions}]
鈫怺2m2021-09-30 15:05:41.225鈫怺0;39m 鈫怺32mTRACE鈫怺0;39m 鈫怺35m16532鈫怺0;39m 鈫怺2m---鈫怺0;39m 鈫怺2m[nio-8080-exec-1]鈫怺0;39m 鈫怺36mo.s.web.servlet.DispatcherServlet       鈫怺0
;39m 鈫怺2m:鈫怺0;39m No view rendering, null ModelAndView returned.
鈫怺2m2021-09-30 15:05:41.232鈫怺0;39m 鈫怺32mDEBUG鈫怺0;39m 鈫怺35m16532鈫怺0;39m 鈫怺2m---鈫怺0;39m 鈫怺2m[nio-8080-exec-1]鈫怺0;39m 鈫怺36mo.s.web.servlet.DispatcherServlet       鈫怺0
;39m 鈫怺2m:鈫怺0;39m Exiting from "ERROR" dispatch, status 500, headers={masked}

Upgrade do Java

Poderia aplicar a atualiza莽茫o do Java no projeto saindo do 16 no-lts e indo par ao 17 LTS

Error when updating user

I am getting the following error when attempting to update a user:
"status":500,"error":"Internal Server Error","trace":"org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

Steps to reproduce it:

  1. curl -H 'Content-Type: application/json' -d '{"email":"[email protected]","password":"admin"}' localhost:8080/api/sessions
  2. curl -H 'Content-Type: application/json' -d '{"name":"name1","email":"[email protected]","password":"12345678"}' -H "Authorization: Bearer <token>" localhost:8080/api/users/
  3. curl -X PUT -H 'Content-Type: application/json' -d '{"name":"chris2","email":"[email protected]"}' -H "Authorization: Bearer <token>" localhost:8080/api/users/{id}

Am I doing something wrong? Or is this a bug in the application?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.