The work in this repository demonstrate standard ways to write rest api using spring boot and data jpa with integration tests
- navigate to repository root
- Make the following scripts executable
chmod +x dockerBuild.sh
,chmod +x runProd.sh
- Build docker image
./dockerBuild.sh
- Run docker image
./runProd.sh
- The app can be accessed in the following url http://localhost:8080/swagger-ui/#/
- Database can be accesed at: http://localhost:8080/h2-console/l
- Project Structure with - JAVA 17, JUnit 5, Spring, Gradle
- Functional Domain Model along with DB Structure
- Configure separate db for dev and test environment
- Integration tests to validate domain mappings
- Configure Github project & CI script to use Github Action as CI Tool
-
Regular Spring MVC instead of Spring reactive webflux. One driving factor of not choosing webflux is lack of reactive support in hibernate. As this project is time limited with very limited resource (Only one developer) I chose to use hibernate for the ease of domain mapping and query generation.
-
This being a simple poc for production I chose embedded H2 database for both dev, prod and test profile. In a real project for dev and prod profile a full fledged RDBMS system (like Oracle, Postgress or Mysql) should be used. For test profile embedded H2 is fine.
-
In case of database schema design, the relation between Item and Category is ManyToMany as I assumed an item can fall into a multiple category and vise versa. On the other hand the relation between
Attribute
andValue
is kept asOneToMany
. For this choice the value items would be repeated. It can be made reusable with mgirations if the business requirement is explained more, and the data quality of attributes and its further usages are well comprehended later. -
Currently
ItemType
is defined by hardcodedEnum
. In an improvement it can be dynamic. Also, if we notice diverse data element of differentItemType
we can create separate tables for different item types with their specialized data. But this approach is needed if bbusiness logic requires tight integration with in features for separate item types.
- API Endpoint according to business specification
- Integration test for Endpoint Controllers
- API validation and exception handling
- Added request param logging interceptor
- Used Spring Data JPA as repository
- There is a scope for improvement while
Category
andItem
but initially to keep the implementation simple didn't use any raw sql or jpql directly. Need to monitor constantly and if we see Hibernate generating excessive select queries for selecting the child elements we can replace Hibernate Object loading with optimized jpql. We can configure Hibernate second level cache for highly queried objects to further tune performance. - We can also cache API response for highly queried endpoints that gets updated less frequenctly
- The response of API endpoints could be more decorated and beautified. Also, a lot more business validation's and formatted validation message could be added. We can use HATEOAS for decorate the API response further and make it more descriptive.
- API documentation can be improved a lot by adding more Swagger config
- Logging can be improved a bit
- All the API endpoint should be protected (ideally with jwt / oauth token). But this should fall into a much broader scope in terms of both authentication and authorization
- CORS origin request should be restricted and api should be able to connect from a specific origin (if not prepared to distribute in mass population). Spring Security has a lot of simple ways to add these restriction.
- Another approach of resource create/update/delete would be make these process asynchronous in background and provide client
with a
HTTP.ACCEPTED
status instead ofHTTP.OK
to let them know the request would be processed shortly. But this overall approach goes very well with event driven architecture. Spring webflux along with Axon framework are two of the nicest tools to develop scalable event driven app. - API field names can be improved & Repository test cases can be improved a lot
- Layered Docker image building script with exploded jar. This layered images are easy to update when change in code or dependency occurs.
- Docker container run script for prod profile with demonstration of property override on runtime for sensitive information
- There are other approaches to override sensitive information. Like if we deploy our application in kubernetes cluster we can configure secrets from there to use as runtime environment variable that eventually will override the existing config, also we can pass external config file explicitly while deploying spring boot jar / exploded jar.