Okay, let's break down a recommended project folder and file structure for a Spring Boot backend application. This structure is widely used, follows Spring Boot conventions, promotes separation of concerns, and is scalable for most common use cases.
It follows a layered architecture within packages.
your-app-name/
├── .gitignore
├── README.md
├── pom.xml (or build.gradle)
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── yourcompany/
│ │ │ └── yourappname/
│ │ │ ├── YourAppication.java <- Main Spring Boot entry point
│ │ │ ├── config/ <- Spring configuration classes
│ │ │ │ └── SecurityConfig.java <- Example config
│ │ │ ├── controller/ <- REST controllers
│ │ │ │ └── UserController.java
│ │ │ ├── service/ <- Business logic services
│ │ │ │ └── UserService.java
│ │ │ ├── repository/ <- Data access repositories
│ │ │ │ └── UserRepository.java
│ │ │ ├── model/ <- Domain models / Entities (JPA)
│ │ │ │ └── User.java
│ │ │ ├── dto/ <- Data Transfer Objects (request/response)
│ │ │ │ ├── UserCreateDto.java
│ │ │ │ └── UserResponseDto.java
│ │ │ ├── exception/ <- Custom exceptions and handlers
│ │ │ │ └── ResourceNotFoundException.java
│ │ │ ├── util/ <- Common utility classes
│ │ │ │ └── DateUtil.java
│ │ │ └── aspect/ <- AOP Aspects (logging, security etc.)
│ │ │ └── LoggingAspect.java
│ │ └── resources/
│ │ ├── application.properties (or application.yml) <- Main application config
│ │ ├── application-dev.yml <- Profile specific config
│ │ ├── static/ <- Static resources (CSS, JS, images)
│ │ ├── templates/ <- Server-side templates (Thymeleaf, etc.)
│ │ └── db/ <- Database related files (e.g., Flyway/Liquibase scripts)
│ │ └── migration/
│ │ └── V1__initial_schema.sql
│ └── test/
│ └── java/
│ └── com/
│ └── yourcompany/
│ └── yourappname/
│ ├── controller/ <- Controller tests
│ │ └── UserControllerTest.java
│ ├── service/ <- Service tests
│ │ └── UserServiceTest.java
│ └── repository/ <- Repository tests
│ └── UserRepositoryTest.java
Explanation of Key Folders and Files:
Root Directory (your-app-name/)
.gitignore: Specifies intentionally untracked files that Git should ignore (e.g., build artifacts, IDE files, logs).
README.md: Project description, setup instructions, usage, etc. Essential documentation.
pom.xml (Maven) or build.gradle (Gradle): Build configuration file. Defines dependencies, plugins, build process.
src/
- Contains all source code and resources. Divided into
main (production code) and test (test code).
src/main/
java/: Contains all your Java source files.
com/yourcompany/yourappname/: This is your base package. Crucially, @SpringBootApplication enables component scanning starting from this package downwards. All your Spring components (@Component, @Service, @Repository, @Controller, @Configuration, etc.) should be within this package or its sub-packages to be automatically discovered.
YourApplication.java: The main class annotated with @SpringBootApplication. Contains the main method to run the application.
config/: Houses Spring @Configuration classes. This is where you define beans, set up integrations (like data sources, message queues), configure security filters, etc., that don't belong to a specific business layer.
controller/: Contains classes handling incoming HTTP requests. Typically annotated with @RestController. They parse request data, call appropriate service methods, and return responses (often DTOs).
service/: Contains the core business logic. Annotated with @Service. Services orchestrate operations, perform validations, and interact with repositories. They define the application's API.
repository/: Contains classes that interact with the data source (database, external API, etc.). Often uses Spring Data interfaces (JpaRepository, CrudRepository). Annotated with @Repository. Handles data persistence and retrieval.
model/ (or entity/ or domain/): Represents the data structures used in your application.
- If using JPA, this is where your
@Entity classes go.
- Can also contain core domain classes that aren't necessarily entities but represent business concepts.
- Note: Sometimes DTOs are placed here, but putting them in a separate
dto package is often cleaner.
dto/: Data Transfer Objects. Classes specifically designed for transferring data between layers or between the application and clients (request bodies, response bodies). They often don't contain business logic or JPA annotations.
exception/: Custom exception classes specific to your application and global exception handling classes (@ControllerAdvice).
src/test/
java/: Contains your test source files.
- Mirror the package structure of
src/main/java. This makes it easy to find the tests for a specific class (e.g., UserServiceTest.java goes in ...yourappname/service/).
- Includes unit tests, integration tests, etc. Use testing frameworks like JUnit, Mockito, Spring Boot Test.
Alternative Structure: Feature-Based
For larger applications, organizing by feature (or module) instead of layer can sometimes improve maintainability.
your-app-name/
├── ... (root & resources same as above)
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── yourcompany/
│ └── yourappname/
│ ├── YourAppication.java
│ ├── config/
│ ├── common/ <- General utilities, exceptions, common DTOs
│ │ ├── util/
│ │ └── exception/
│ ├── user/ <- Feature: User Management
│ │ ├── UserController.java
│ │ ├── UserService.java
│ │ ├── UserRepository.java
│ │ ├── User.java (model)
│ │ └── dto/ (User specific DTOs)
│ │ ├── UserCreateDto.java
│ │ └── UserResponseDto.java
│ ├── order/ <- Feature: Order Processing
│ │ ├── OrderController.java
│ │ ├── OrderService.java
│ │ ├── OrderRepository.java
│ │ ├── Order.java (model)
│ │ └── dto/ (Order specific DTOs)
│ └── product/ <- Feature: Product Catalog
│ ├── ProductController.java
│ ├── ProductService.java
│ ├── ProductRepository.java
│ ├── Product.java (model)
│ └── dto/ (Product specific DTOs)
- Pros (Feature-Based): Higher cohesion within feature packages, lower coupling between features, easier to understand the scope of a change (most changes for a feature are within its package), better for large teams working on different features.
- Cons (Feature-Based): Can lead to some duplication (e.g., common DTOs might be spread out or need a separate
common package), finding a file requires knowing which feature it belongs to.
Which structure to choose?
- The layered structure is the most common and a great default for most small to medium-sized Spring Boot applications. It's easy to understand and maps well to the common architectural layers.
- The feature-based structure is often preferred for larger, more complex applications with distinct, relatively independent business domains (features).
Tips for Good Practice:
- Consistency: Once you choose a structure, stick to it consistently throughout the project.
- Naming: Use clear and consistent naming conventions (e.g.,
...Controller, ...Service, ...Repository, ...Test).
- Avoid Deep Nesting: Don't create excessively deep package structures (e.g.,
com.yourcompany.yourappname.core.domain.entities.user.models). Keep it reasonably flat.
- Keep Packages Clean: Each package should have a clear purpose, as outlined above.
- README is Key: Always include a README explaining how to build and run the project.
This recommended structure provides a solid foundation for building maintainable and well-organized Spring Boot backend applications. Choose the layered or feature-based approach depending on the project's size and complexity.