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