Giter VIP home page Giter VIP logo

spring-rest-api-starter's Introduction

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

spring-rest-api-starter's People

Contributors

dependabot[bot] avatar trinhdvt avatar

Stargazers

 avatar

Watchers

 avatar

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.