Spring Boot Project Structure for Better Readability

Posted By : Krishna Verma | 30-Nov-2018

Quick Summary or Introduction:
Hello everyone in this blog I am going to discuss the project structure You have to follow which will result in great readability and efficiency so whenever a third person will read your code or someone also have to work on your code they will understood your code easily and if there are some changes to be made in the messages or error codes it can be easily managed.


Prerequisites (Software or Hardware)
1. java 1.8.0 or about
2. spring boot (STS) for development.
3. Sublime Text editor (Optional).

Multiple Steps Explanation:
Before start working on a project, its planning and its Design pattern or structure
is very important So we can follow a global approach which is language independent.so I am breaking down all these information in points so that it can be easy for understanding.

1. Project Name:
   Before Making a New project or a new Micro-Service proper project name or Micro-Service name should be used which describes the functionality of the project or Service.

2. Packages Names:
   You must have to divide your project in small-small Module so that you can easily find  file specific to the functionality of the file, below are the example of the project structure which You can follow we are taking our project name (myproject) here are the points:
 
a) projectName.MicroServiceName (for eg. mycompany.myproject.microservicename):
    This is the Default package which will be created automatically when you set up your first spring boot project it contains your main method.
    
 b) myProject.microServiceName.config (for eg. mycompany.myproject.microServiceName.config):
     This package is used for the configuration purpose whether its third party or internal for the third party like Redis configuration, Kafka configuration, Bitcoin configuration,litecoin configuration etc.
    
 c) myProject.microServiceName.security (for eg. mycompany.myproject.microServiceName.security).
     This package contains all the files related to the security of the project like spring security,jwt security.

 d) myProject.microServiceName.constant (for eg. mycompany.myproject.microServiceName.constant).
   In this Package, you will Manage all the files related to the constant, like the constant message, URLs for API so that if something will have to change it can be easily achieved.
   
 e) myProject.microServiceName.controller (for eg. mycompany.myproject.microServiceName.controller).
    In this Package We will write code for Creating api based on the functionality.

 f) myProject.microServiceName.dto (for eg. mycompany.myproject.microServiceName.dto).
    In this Package, we will keep files of DTO for eg. what kind of data we required in API or kind of response we want to return.


g) myProject.microServiceName.enums (for eg. mycompany.myproject.microServiceName.enums).
    In this package, we will keep our enums constant so that we can find them easily whenever we need them.

 h) myProject.microServiceName.exceptions(for eg. mycompany.myproject.microServiceName.exceptions).
    In this Package, we will keep all the files which are related to the custom exceptions.    
  
 i) myProject.microServiceName.http (for eg. mycompany.myproject.microServiceName.http).
    In this package, You can keep files of configuration regarding the HTTP calls to other micro-Services like feign client call to communicate with other micro-Service.

 j) myProject.microServiceName.model (for eg. mycompany.myproject.microServiceName.model).
    In this Package, You can keep all the files related to the Model of the Database structure. 

 k) myProject.microServiceName.repository (for eg. mycompany.myproject.microServiceName.repository).
    In this package, you can keep all the files related to the MySQL queries to fetch data from the repository or Database.

 l) myProject.microServiceName.service (for eg. mycompany.myproject.microServiceName.service).
    In this package You can keep all the files related to the Business Logic
,it will contain all the classes having methods with some business logic.

 m) myProject.microServiceName.util (for eg. mycompany.myproject.microServiceName.util).
    In this package, You can keep Your files which having some utility methods which are mostly used like checking string null or empty, objects comparison etc.

 

Conclusion:

if You follow this project structure pattern it will be easy for You and new developers to understand the project in a better way just like writting best code is consider as skills,managing code structure also plays a important role.

 

About Author

Author Image
Krishna Verma

Krishna is a Software Developer having key skills in java , his hobbies are learning new technologies

Request for Proposal

Name is required

Comment is required

Sending message..