Spring Boot Project Structure Best Practices




Hey guys in this post, we will discuss structuring Spring boot application and their best practices.

Read More:

Overview


Its always good practice to structure your Spring boot application while developing production grade applications. If we don’t structure it properly then its very difficult to maintain and search the files as our application grows. So today in this post, I will show you how I structure Spring boot application and I will tell you the best practices.

Note: This is my way of structuring the application, you can follow the same structure or you can change the structure as per your needs.
16

This is my typing Spring boot project structure. Let’s discuss these packages one by one in detail.

Base Package


Spring boot application always comes with base package, for example in.bushansirgur.springbootrestapi which contains the base class SpringbootrestapiApplication.java which contains the main() method. This is the starting point to spring boot application.

package in.bushansirgur.springbootrestapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootrestapiApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootrestapiApplication.class, args);
	}

}

Auth Package


This is the package in.bushansirgur.springbootrestapi.authmanage, where Login and Logout code is present.

  • AuthRequest.java is basically a POJO contains private fields such as username, email and password
  • AuthController.java is basically accepts HTTP request for login and logout
  • AuthService.java is basically interface contains the business logic for authorizing and authenticate the user

Constants Package


This is the package in.bushansirgur.springbootrestapi.constants, where the contants and enums will be present.

  • ApplicationConstants.java is basically a class contains, public static final fields, which represents the constant variables used in the application
  • SecurityConstant.java is basically an enum, contains the constant variables for security

Email Package


Assume that in your application, there is a email functionality then its better to keep it in a separate package in.bushansirgur.springbootrestapi.email

  • EmailService.java this is basically an interface which contains methods for sending emails.




  • SendGridService.java this is basically an implementation for EmailService, contains the implementation for sending emails.

Employee Package


Assume that you are building REST API for employee management then its always to good practice to keep all the employee functionality in one package in.bushansirgur.springbootrestapi.employee

  • EmployeeRequest.java this is basically model class contains private fields, which accepts the form data
  • EmployeeResponse.java this is also model class which contains the private fields, this is the final object that sent back to client
  • EmployeeEntity.java this is basically entity class which is the used to persist the data to database
  • EmployeeController.java this is the employee controller which accepts HTTP requests
  • EmployeeService.java this is an interface contains the business logic for employee resource
  • EmployeeDAO.java this is an interface contains the database operations such as create, read, update and delete

FileUpload Package


Assume that in your application there is a file upload functionality then its better to keep all the file upload functionality code in one package in.bushansirgur.springbootrestapi.fileupload

  • FileService.java this is an interface contains the abstract methods for uploading files to cloud or database
  • AWSS3Service.java this is an implementation for FileService which contains the implementation for uploading files to AWS S3 bucket.

Security Package


This is the package in.bushansirgur.springbootrestapi.security, which contains the files for security related functionality.

  • SecurityConfig.java this is the class where security configuration goes in.

Util Package


This is the package in.bushansirgur.springbootrestapi.util which contains the files for utility related functionality.

  • DateAndTimeUtil.java this is the file which contains the util methods for date and time
  • JwtUtil.java this is the file contains util methods for Json web tokens

Similarly, you can add more packages and classes as per your application needs. This is how I organize my spring boot application.

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply