The only thing better than a Maven archetype is a repo you can fork with everything already setup, just fork-and-code.
This repository contains a recipe/scaffolding for bootstrapping a Web Application with the features & Technology stack listed below. Delete the sample code (or keep it.) and add your own, you’re good to go.
- Application screenshots
- Internationalization (i18n)
- Tech stack & Open-source libraries
- To-Do
- Running the application locally
- Running the application via docker container
- Security
- Explore Rest APIs
- Sample Valid JSON Request Bodys * Create Person -> /api/person
- Documentation
- EER Diagram
- Files and Directories Structure
- Packages
- Reporting Issues/Suggest Improvements
- FOSSA third-party code, license compliance and vulnerabilities
This app can be adapted to various languages and regions without engineering changes. Textual elements, such as status messages and the GUI component labels, are not hardcoded in the program. Instead they are stored outside the source code and retrieved dynamically.
Refer io.github.anantharajuc.sbtest.util.config.I18Nconfiguration
. The text elements are stored in \src\main\resources\i18n
folder.
- Bootstrap - Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development.
- Bootstrap Table - An extended table to the integration with some of the most widely used CSS frameworks.
- Thymeleaf - Modern server-side Java template engine for both web and standalone environments.
- JDK - Java™ Platform, Standard Edition Development Kit
- Spring Boot - Framework to ease the bootstrapping and development of new Spring Applications
- Maven - Dependency Management
- Bootstrap ToC - Table of Contents plugin for Bootstrap
- Thymeleaf With Dialect - A dialect for Thymeleaf that allows you to use attributes with a "with" prefix to avoid having long "th:with"-expressions.
- Thymeleaf Layout Dialect - A dialect for Thymeleaf that lets you build layouts and reusable templates in order to improve code reuse.
- Lombok - Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
- Swagger - Open-Source software framework backed by a large ecosystem of tools that helps developers design, build, document, and consume RESTful Web services.
- Bucket4j - Java rate limiting library based on token/leaky-bucket algorithm.
- git - Free and Open-Source distributed version control system
- Prometheus - Monitoring system and time series database
- Postman - API Development Environment (Testing Docmentation)
- Postman Echo - A service that can be used to test your REST clients and make sample API calls. It provides endpoints for GET, POST, PUT, various auth mechanisms and other utility endpoints.
- Travis CI - A hosted continuous integration service used to build and test software projects hosted at GitHub and Bitbucket.
- Codecov - A hosted tool that is used to measure the test coverage of your codebase.
- Dependabot - Automated dependency updates.
- FOSSA - Scalable, end-to-end management for third-party code, license compliance and vulnerabilities.
- sonarcloud - Cloud-based code analysis service designed to detect code quality issues continuously ensuring the maintainability, reliability and security of code.
- Logger (Console, File)
- RESTful Web Service (CRUD)
- Content Negotiation
- Dark Mode
- Spring Security RBAC, Session Timeout
- API Rate Limiting
- JPA Auditing via AuditorAware Interface
- Spring Profiles
- Docker
- Caching
- Shut down app on button click via actuator url
- HATEOS
- Spring Boot Admin
- NoSQL (MongoDB)
- Multitenancy
- Micrometer
- Grafna
There are several ways to run a Spring Boot application on your local machine. One way is to execute the main
method in the com.arc.sbtest.SBtemplateApplication
class from your IDE.
- Download the zip or clone the Git repository.
- Unzip the zip file (if you downloaded one)
- Open Command Prompt and Change directory (cd) to folder containing pom.xml
- Open Eclipse
- File -> Import -> Existing Maven Project -> Navigate to the folder where you unzipped the zip
- Select the project
- Choose the Spring Boot Application file (search for @SpringBootApplication)
- Right Click on the file and Run as Java Application
Alternatively you can use the Spring Boot Maven plugin like so:
mvn spring-boot:run
The code can also be built into a jar and then executed/run. Once the jar is built, run the jar by double clicking on it or by using the command java -jar SBtemplate-0.0.1-SNAPSHOT.jar
To shutdown the jar, follow the below mentioned steps on a Windows machine.
- In command prompt execute the jcmd command to print a list of all running Java processes
- Taskkill /PID PROCESS_ID_OF_RUNNING_APP /F execute this command by replacing the PROCESS_ID_OF_RUNNING_APP with the actual process id of the running jar found out from executing the previous command
The app will start running at http://localhost:8080, change the database settings in application.properties file as per your need.
- anantha/spring-boot-application-template - DockerHub Image
DockerHub Pull Command
docker pull anantha/spring-boot-application-template
Ensure you build a jar of the application before building a docker image.
`mvn package -Dmaven.test.skip=true` //skip all tests and build. The build once completed is available in target folder
`mvn clean package` //run all tests and build
On Windows machine use Windows Powershell, navigate to the project folder where Dockerfile is present.
Command | Description |
---|---|
docker images |
take a look at the container images. |
docker ps |
list all the running containers. |
docker ps -a |
list all the containers, including the ones that have finished executing. |
docker build -t spring-boot-application-template . |
Build docker image of the project |
docker run spring-boot-application-template |
run the project's docker container |
docker stop [container_id] |
stop a container |
docker rm $(docker ps -aq) |
stop and remove all containers |
Role Based Authentication (In-memory Users)
Permission Based Authentication (In-memory Users)
Spring Method-Security with @PreAuthorize (In-memory Users)
Database Authentication Authorization (In-memory Users)
Role, Permission based User Authentication via MySQL
Refer to the ApplicationSecurityConfig
class in io.github.anantharajuc.sbtest.security
.
Username | Password | Role | Permission | Resource |
---|---|---|---|---|
johndoe |
password |
PERSON |
/api/v1/person |
|
AdminUser |
password |
ADMIN |
PERSON_CREATE,PERSON_READ,PERSON_UPDATE,PERSON_DELETE |
/management/api/v1/person |
AdminTraineeUser |
password |
ADMINTRAINEE |
PERSON_READ |
/management/api/v1/person |
Bucket4j - Rate limiting library based on token/leaky-bucket algorithm - Refer io.github.anantharajuc.sbtest.api.rate_limiting package
A basic solution for preventing brute force authentication attempts using Spring Security is implemented. The app keeps a record of the number of failed attempts originating from a single IP address. If that particular IP goes over a set number of requests – it will be blocked for a set amount of time.
Refer io.github.anantharajuc.sbtest.security.authentication.LoginAttemptService
If the application remains inactive for a specified period of time, the session will expire. The session after this period of time is considered invalid and the user has to login to the application again.
This value server.servlet.session.timeout can be configured in application.properties file
The app defines following CRUD APIs.
URL | Method | Remarks |
---|---|---|
http://localhost:8080/index |
GET | Home Page |
http://localhost:8080/sbat/index |
GET | Home Page |
http://localhost:8080/sbat/about |
GET | About Page |
http://localhost:8080/sbat/tech-stack |
GET | Technology Stack Table |
http://localhost:8080/sbat/close |
GET | Close App via Actuator |
http://localhost:8080/sbat/login |
GET | Login Page |
http://localhost:8080/sbat/error |
GET | Custom Error Page |
URL | Method |
---|---|
http://localhost:8080/api/generic-hello |
GET |
http://localhost:8080/api/personalized-hello/ |
GET |
http://localhost:8080/api/personalized-hello?name=spring-boot |
GET |
http://localhost:8080/api/loggers |
GET |
To monitor and manage your application
URL | Method |
---|---|
http://localhost:8080/actuator/ |
GET |
http://localhost:8080/actuator/health |
GET |
http://localhost:8080/actuator/info |
GET |
http://localhost:8080/actuator/prometheus |
GET |
http://localhost:8080/actuator/httptrace |
GET |
URL | Method | Remarks | Sample Valid Request Body |
---|---|---|---|
http://localhost:8080/api/v1/person |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
|
http://localhost:8080/api/v1/person |
POST | Add a person | JSON |
http://localhost:8080/api/v1/person/{id} |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
|
http://localhost:8080/management/api/v1/person/pageable |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
Pageable API Endpoint |
http://localhost:8080/api/v1/person/{id} |
PUT | Update a person | JSON |
http://localhost:8080/api/v1/person/{id} |
DELETE | Delete a person |
URL | Method | Remarks | Sample Valid Request Body |
---|---|---|---|
http://localhost:8080/management/api/v1/person |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
|
http://localhost:8080/management/api/v1/person |
POST | Add a person | JSON |
http://localhost:8080/management/api/v1/person/{id} |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
|
http://localhost:8080/management/api/v1/person/pageable |
GET | Header Accept:application/json or Accept:application/xml for content negotiation |
Pageable API Endpoint |
http://localhost:8080/management/api/v1/person/{id} |
PUT | Update a person | JSON |
http://localhost:8080/management/api/v1/person/{id} |
DELETE | Delete a person |
{
"name": "Jane",
"username": "janejane",
"emailPrimary": "[email protected]",
"emailSecondary": "[email protected]",
"phone":9191919191,
"gender": "FEMALE",
"age": 25,
"password": "password",
"dob":"25-12-2005",
"isAdult":true,
"address": {
"street": "Jane Plains",
"suite": "Suite 779",
"city": "Wisokyburghh",
"zipcode": "90565-7771",
"geo": {
"lat": "-43.9589",
"lng": "-34.4628"
}
}
}
- Postman Collection - online, with code auto-generated snippets in cURL, jQuery, Ruby,Python Requests, Node, PHP and Go programming languages
- [Postman Collection](Spring Boot Application Template.postman_collection) - offline
- Swagger -
http://localhost:8080/swagger-ui.html
- Documentation & Testing - Swagger -
http://localhost:8080/v2/api-docs?group=Spring%20Boot%20Application%20Template
- Documentation & Testing - Find Java Doc in javadoc folder
- Java Doc is generated in
Spring-Boot-Application-Template\target\site\apidocs
folder using the Maven command
`mvn javadoc:javadoc` //Generate JavaDoc
The project (a.k.a. project directory) has a particular directory structure. A representative project is shown below:
.
├── Spring Elements
├── src
│ └── main
│ └── java
│ ├── io.github.anantharajuc.sbtest
│ │ │
│ │ ├──io.github.anantharajuc.sbtest.auditing
│ │ │
│ │ ├──io.github.anantharajuc.sbtest.security.authorization
│ │ │
│ │ ├──io.github.anantharajuc.sbtest.backend
│ │ │ │
│ │ │ ├──io.github.anantharajuc.sbtest.person
│ │ │ │
│ │ │ └──io.github.anantharajuc.sbtest.backend.persistence.repositories
│ │ │
│ │ └──io.github.anantharajuc.sbtest.backend.service
│ │
│ ├── io.github.anantharajuc.sbtest.util.config
│ │
│ ├── io.github.anantharajuc.sbtest.enums
│ │
│ ├── io.github.anantharajuc.sbtest.exception
│ │
│ ├── io.github.anantharajuc.sbtest.security
│ │
│ ├── io.github.anantharajuc.sbtest.util
│ │
│ └── io.github.anantharajuc.sbtest.web
│ │
│ ├── io.github.anantharajuc.sbtest.web.controllers
│ │
│ └── io.github.anantharajuc.sbtest.web.domain.frontend
├── src
│ └── main
│ └── resources
│ ├── data
│ │ └── mysql
│ │ └── migrations
│ │ ├── V0_0_1__initialize_structure.sql
│ │ └── V0_0_2__audit_structure.sql
│ │ └── V0_0_3__populate_data.sql
│ │ └── V0_0_4__data_geo.sql
│ │ └── V0_0_5__data_address.sql
│ │ └── V0_0_6__data_person.sql
│ │ └── V0_0_7__data_security.sql
│ ├── i18n
│ │ └── messages.properties
│ │ └── messages_es.properties
│ ├── static
│ │ ├── css
│ │ ├── images
│ │ ├── js
│ │ └── favicon.ico
│ ├── templates
│ │ ├── fragments
│ │ │ ├── body_scripts.html
│ │ │ ├── footer.html
│ │ │ ├── htmlhead.html
│ │ │ ├── navigation.html
│ │ │ ├── pagetitle.html
│ │ │ └── social_buttons.html
│ │ │
│ │ ├── pages
│ │ │ ├── about.html
│ │ │ ├── built_with.html
│ │ │ ├── close.html
│ │ │ ├── form.html
│ │ │ ├── index.html
│ │ │ ├── login.html
│ │ │ └── settings.html
│ │ │
│ │ ├── error.html
│ │ └── layout.html
│ │
│ ├── application-dev.properties
│ ├── application-production.properties
│ ├── application-qa.properties
│ ├── application-staging.properties
│ ├── application.properties
│ ├── banner.txt
│ └── log4j2.xml
├── src
│ └── test
│ └── java/io/github/anantharajuc/sbtest/service
│ └── PersonServiceImpl.test
├── JRE System Library
├── Maven Dependencies
├── bin
├── logs
│ └── application.log
├── src
├── target
│ └──application-0.0.1-SNAPSHOT
├── mvnw
├── mvnw.cmd
├── pom.xml
└── README.md
-
api
- API utilities; -
rate_limiting
- API rate limiting; -
auditing
- data entity auditing; -
authentication
- application user authentication; -
configuration
- app configurations; -
controllers
- to listen to the client; -
exception
- to hold custom exception handling; -
models
- to hold our entities; -
repository
- to communicate with the database; -
security
- security configuration; -
service
- to hold business logic; -
util
- to hold our utility classes; -
resources/
- Contains all the static resources, templates and property files. -
resources/data/mysql.migrations/
- Contains initial table structure & table data - used by flyway. -
resources/static
- contains static resources such as css, js and images. -
resources/templates
- contains server-side templates which are rendered by Spring. -
resources/templates/fragments
- contains reusable code fragments. -
resources/templates/pages
- contains server-side templates built using fragments. -
resources/application.properties
- It contains application-wide properties. Spring reads the properties defined in this file to configure your application. You can define server’s default port, server’s context path, database URLs etc, in this file. -
test/
- contains unit and integration tests -
pom.xml
- contains all the project dependencies
This Project uses GitHub's integrated issue tracking system to record bugs and feature requests. If you want to raise an issue, please follow the recommendations below:
- Before you log a bug, please search the issue tracker to see if someone has already reported the problem.
- If the issue doesn't already exist, create a new issue
- Please provide as much information as possible with the issue report.
- If you need to paste code, or include a stack trace use Markdown +++```+++ escapes before and after your text.