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.
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 |
-
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
- An
-
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 useSpring Cache
to cache data
- An
-
Logging
- Logging with
Slf4j
fromLombok
- AOP profiling of all Spring beans method calls (e.g. method parameters, return values, errors)
- Logging with
-
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
- Custom
-
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 EmailAssertUtils
to support common methods in validating parameterSecurityUtils
to support common methods to work with Spring Security
-
Dockerfile to build a production-ready image
- 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)
- Use this repository as a template and create your GitHub repository
- Search and replace "spring-rest-api-starter", "example" with your project name
- Edit the properties in
application.yml
andapplication-private.yml
based on your need (e.g. database, email, etc.) - 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 .
If you want to ask anything, create an Issue, or contact me @dvt
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