Sprint Boot Rest API Boilerplate with JPA - Swagger - Actuator & other commonly used spring dependencies.
Table of contents generated with markdown-toc
This project is designed to make it easier to understand spring boot and different plugins / libraries it provides to design and implement crisp - clean - robust REST API.
Having modules or packages makes it easier for us to logically understand or group different components in our code. All
different classes and interfaces under same module or package will have exactly similar function set (in broad sense)
.
This boilerplate template has the following modules / packages:
- models
- entities
- repositories
- servlets
- controllers
This package or module is responsible for containing all the data entities with repositories which will be used by Hibernate & JPA to work with connected Database.
The data entities are essentially Table Row Definitions for Tables present in our database. Users are encouraged to define all the JPA and Hibernate related Data Entities here under this package.
The defined data entities need repositories for Hibernate - JPA to correctly and efficiently retrieve information from JDBC Datasources. Users are encouraged to define their CRUD Repositories associated with data entities under this package.
Servlets are supposed to have complex logic which we would like to use inside controllers. This extra layer has been added since often there is a requirement for having some medium complex logics which we would like to re-use.
Instead of placing this code in controllers, it's desirable to place it inside servlets. Since auto-wiring servlets into multiple controllers and testing it becomes fairly simpler.
This is the actual controller where we are defining actual route bindings to java functions using Spring Boot annotations. Ideally apart from information related to route definition, there should not be any kind of logic happening here. The controller code should make calls to servlets where the actual route's application logic is present.
# execute all tests
mvn clean test
# test and generate shaded jar
# file in target folder
mvn clean install
# execute the jar generated
# in target folder
java -jar target/spring-boot-rest-jpa-*.jar
SWAGGER UI (app must be running in localhost)
To use this service / rest code please make sure you complete the following:
- update the
pom.xml
file to have correct artifact and org names. - update the package to be same as your organization.
- test everything is working as expected with H2DB (
mvn clean test
). - update
pom.xml
and add your jdbc driver - update
application.properties
and correctly configure your jdbc configuration. - create new model - repository for any 1 table in your actual data source.
- write unit tests for newly created repository.
- verify new unit test works as expected
(only run tests for new repository)
- create new servlet - controller associated with selected table
- write unit tests for newly created servlet and controller.
- verify new unit tests work as expected
(only run tests for new controller & servlet)
- remove sample (or) example models - repository - servlets - controllers.
- remove or update demo method in
AppEntryPoint.java
class since it was present to add sample data to H2DB. - run tests and ensure everything is working as expected.