Skip to content

A "simple" template to start a REST API Project with Spring πŸš€

License

Notifications You must be signed in to change notification settings

trinhdvt/spring-rest-api-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spring REST API Starter πŸš€

Table of Contents

What is this repository

This repository is a starting point for a Spring REST API project. It contains all necessary configurations to get a working REST API up and running.

Dependencies

Click to expand!
Dependency Version Description
Spring Boot 2.4.5 Config Spring-app with Java Annotation
Spring Web MVC 2.4.5 Build REST API endpoint
Spring Security 2.4.5 Securing application
Spring Data JPA 2.4.5 Connect database and manipulate data
Spring Mail 2.4.5 Send email
Spring WebSocket 2.4.5 Build WebSocket server
Spring Messaging 2.4.5 Send message over WebSocket
Spring Cache 2.4.5 Caching data
Spring AOP 2.4.5 Support Aspect-Oriented Programming
Caffeine 2.9.0 Memcached library for Java, use for control cached data
MySQL 8.x Database for application
Lombok 1.18.22 Generate common method to reduce boilerplate code
Java Validation API 2.0.1 Validate parameter
JJWT 0.9.1 Creating and verifying JSON Web Token in Java
Apache Common Text 1.9 Contains several useful methods to work with String
Bucket4j 7.0 For implement API Rate Limit feature

Features

  • Language level and runtime JDK is Java 8

  • Spring EcoSystem 2.4.5

  • Persistence:

    • MySQL 8.0
    • Hibernate 5.4.30
    • Example Entity:
      • An Account entity to store basic information of user
  • REST API with Spring Web MVC

    • An AuthController to demonstrate how to authenticate user with JWT
    • An ExampleController to demonstrate how to use @PreAuthorize to restrict access to endpoint and use Spring Cache to cache data
  • Logging

    • Logging with Slf4j from Lombok
    • AOP profiling of all Spring beans method calls (e.g. method parameters, return values, errors)
  • Security

    • Authentication with JSON Web Token
    • Block too many requests from the same IP Address in a duration of time (API Rate Limit)
    • CORS configuration
    • Annotations to restrict endpoints to certain roles
    • Custom CurrentUser to get current user information from JWT token
  • Exception Handling

    • Custom HttpError to represent error response
    • Automatic exception handling with GlobalExceptionHandler class
  • Several utility classes to support common task

    • MyStringUtils to support common methods to work with String (e.g. escape HTML, generate slug, etc.)
    • MyMailSender to support common methods to work with Email
    • AssertUtils to support common methods in validating parameter
    • SecurityUtils to support common methods to work with Spring Security
  • Dockerfile to build a production-ready image

How to use this repository

Prerequisites

  • Globally install Java 8
  • Install your database engine (e.g. MySQL 8.0, MariaDB, etc.) and edit application-private.yml depend on your need ( see Installation )
  • Install Docker (optional)

Installation

  1. Use this repository as a template and create your GitHub repository
  2. Search and replace "spring-rest-api-starter", "example" with your project name
  3. Edit the properties in application.yml and application-private.yml based on your need (e.g. database, email, etc.)
  4. Install maven dependencies with ./mvnw clean install

If no error, you are good to go. Now, start coding and run it with ./mvnw spring-boot:run.

To build Docker image, you need to install Docker and simply run docker build -t <YOUR_DOCKER_HUB_USERNAME>/<YOUR_PROJECT_NAME>:latest .

Contact

If you want to ask anything, create an Issue, or contact me @dvt

Appendix

Project structure

Click to expand!
β”œβ”€β”€ Dockerfile -> Dockerfile to build Docker image
β”œβ”€β”€ README.md
β”œβ”€β”€ mvnw
β”œβ”€β”€ mvnw.cmd
β”œβ”€β”€ pom.xml -> Dependency management
└── src -> Source code
    └── main
        β”œβ”€β”€ java
        β”‚ └── com
        β”‚     └── example
        β”‚         β”œβ”€β”€ ExampleApplication.java -> Entry point of application
        β”‚         β”œβ”€β”€ ServletInitializer.java
        β”‚         β”‚
        β”‚         β”œβ”€β”€ annotation -> Custom annotation
        β”‚         β”‚ └── validator -> Custom anntation's processor
        β”‚         β”‚
        β”‚         β”œβ”€β”€ config -> Application basic configuration (e.g. web, cache, etc.)
        β”‚         β”‚ β”œβ”€β”€ CacheConfig.java
        β”‚         β”‚ β”œβ”€β”€ WebConfig.java
        β”‚         β”‚ └── ...
        β”‚         β”‚
        β”‚         β”œβ”€β”€ controller -> REST API Endpoint Definition
        β”‚         β”‚ └── ExampleController.java
        β”‚         β”‚
        β”‚         β”œβ”€β”€ event
        β”‚         β”‚ └── listener -> Handle event
        β”‚         β”‚     └── AccountEntityListener.java
        β”‚         β”‚
        β”‚         β”œβ”€β”€ exception
        β”‚         β”‚ β”œβ”€β”€ GlobalExceptionHandler.java -> Exception handler
        β”‚         β”‚ β”œβ”€β”€ HttpError.java -> Custom error response
        β”‚         β”‚ └── ...
        β”‚         β”‚
        β”‚         β”œβ”€β”€ model -> Domain model, DTO, etc.
        β”‚         β”‚ β”œβ”€β”€ constraints
        β”‚         β”‚ β”œβ”€β”€ dto
        β”‚         β”‚ β”‚ β”œβ”€β”€ request
        β”‚         β”‚ β”‚ └── response
        β”‚         β”‚ └── entity
        β”‚         β”‚
        β”‚         β”œβ”€β”€ repository -> Data Access Layer
        β”‚         β”‚ └── AccountRepository.java
        β”‚         β”‚ 
        β”‚         β”œβ”€β”€ security -> Security configuration
        β”‚         β”‚ β”œβ”€β”€ SecurityConfig.java
        β”‚         β”‚ β”œβ”€β”€ ...
        β”‚         β”‚ β”œβ”€β”€ filter
        β”‚         β”‚ β”œβ”€β”€ ratelimit
        β”‚         β”‚ └── services -> Security services (seperate from Bussiness serivce)
        β”‚         β”‚     β”œβ”€β”€ TokenServices.java
        β”‚         β”‚     └── ...
        β”‚         β”‚
        β”‚         β”œβ”€β”€ services -> Business logic
        β”‚         β”‚ β”œβ”€β”€ IAuthServices.java
        β”‚         β”‚ └── impl
        β”‚         β”‚     └── AuthServicesImpl.java
        β”‚         β”‚
        β”‚         └── utils -> Utility class
        β”‚             β”œβ”€β”€ AssertUtils.java
        β”‚             └── ...
        β”‚
        └── resources -> Application resources (e.g. spring config file, static files, etc.)
            β”œβ”€β”€ application-private.yml -> Sensitive configuration
            β”œβ”€β”€ application.yml -> Common configuration