Spring Boot Folder Structure (Best Practices)
What is Spring Boot — If you don’t know what spring boot is, I’m happy to share my previous article which is related to spring boot setup. https://medium.com/@malshani-wijekoon/setup-spring-boot-5-x-x-application-using-java-11-0-bb1ebc836996
As a best practise software develepment industry use a well-organized folder structure to improves code maintainability, collaboration among team members, and the overall development process. Here are some key reasons why the Spring Boot folder structure is important:
- Clarity and Organization: A well-defined folder structure provides a clear layout of where to find different components of your application. It makes it easier for developers to locate specific files, packages, and resources, reducing the time spent searching for code.
- Modularity: Spring Boot follows a modular approach where the application is divided into different logical components like controllers, services, repositories, configurations, etc. Having a proper folder structure allows you to organize these components effectively, making the application easier to understand and maintain.
- Separation of Concerns: The folder structure enforces a separation of concerns, allowing developers to focus on specific parts of the application without worrying about unrelated code. For example, business logic resides in the service layer, data access code in the repository layer, and web request handling in the controller layer.
- Scalability: As the application grows, a well-organized folder structure makes it easier to accommodate new features and functionalities without turning the project into an unmanageable mess. It facilitates the addition of new modules or components while maintaining a coherent overall architecture.
- Code Reusability: Proper organization encourages the creation of reusable components. When developers can easily find and understand existing code, they are more likely to reuse it instead of duplicating efforts.
- Ease of Collaboration: When multiple developers work on a project, adhering to a standardized folder structure ensures consistency and facilitates collaboration. Everyone on the team knows where to place new code and can quickly locate code added by others.
- Build and Deployment: Tools like build systems, continuous integration, and deployment scripts can take advantage of a predictable folder structure to automate the build and deployment processes efficiently.
- Testing and Debugging: An organized folder structure makes it simpler to write tests and debug the application. Test classes and resources can be placed alongside the corresponding classes, making it easy to understand the test suite’s coverage.
- Onboarding New Team Members: When new developers join the team, a well-defined folder structure helps them get up to speed quickly. They can quickly understand the architecture and locate relevant code sections.
- Maintainable Codebase: A clean and well-structured codebase is easier to maintain in the long run. It reduces technical debt and makes it less likely to introduce bugs or regressions when making changes.
Following image shows the well recognized folder stucture for Spring Boot application.
Here I’m going to explain the files that need to include above folders.
- Config: Contains configuration classes, where you configure application settings, or
AppConfig
for other application-level configurations.
2. Controller: Contains your RESTful controller classes. These classes handle incoming HTTP requests and define the API endpoints.
3. DTO (Data Transfer Object): A DTO, is a design pattern used to transfer data between different layers or components of an application. The main purpose of a DTO is to encapsulate data and provide a simple data structure that can be easily passed around the application. DTOs are often used to transfer data between the front-end and back-end of a web application, between microservices, or between different layers of an application, like the service layer and the presentation layer.
Characteristics of a DTO:
- It typically contains only private fields with getters and setters to access the data.
- DTOs do not contain any business logic, and their primary focus is to carry data.
- They are often used to represent a subset of data from an entity or a combination of data from multiple entities.
- DTOs help to reduce the amount of data transferred over the network, improving performance by avoiding excessive data exchange.
Example:
4. Enum (Enumeration class): Enum classes are typically used to represent a set of closely related and pre-defined values. Examples of common use cases for Enum classes include request status like SUCCESS, FAIL, PENDING and more.
5. Model: The model folder stores data models or entities that represent structure and behaviour of the application domain. These classes are mapped to database tables and define the properties and relationships of the application data.
6. Repository: Contains repository classes that deal with data access. These classes typically use an ORM (Object-Relational Mapping) framework or JPA (Java Persistence API) to interact with the database.
7. Service: Contains service classes that implement business logic. Controllers use these services to perform operations on data.
8. Util (Utilities): The “util” folder is not specific to Spring Boot; it is a general practice followed in many programming languages and frameworks. In Spring Boot projects, you might find a “util” package or folder where developers place utility classes to keep the codebase organized and modular.
Other than src/main/java folders there are other folders that need to have in a spring boot application. There are as follows.
- src/main/resources: This folder contains non-Java resources like static files, templates, and configuration files.
- src/test: This folder contains all your test classes. Inside this folder there is another folder which is same to src/main/java folder structure. As an example src/test/java/service folder contains test classes for testing the service classes of src/main/java/service classes.
Note: This folder structure can vary from company to company. But this is the basic folder structure of a Spring Boot application.