
A well-organized Spring Boot project structure:
- Makes your code easier to understand and maintain
- Helps collaborators navigate quickly
- Enables smoother testing, deployment, and scaling
Spring Boot projects follow a convention-over-configuration model, which gives you a standard folder layout.
Letβs break it all down π
π Default Spring Boot Directory Layout
When you generate a project (via Spring Initializr), this is what you get:
myproject/
βββ pom.xml
βββ src/
β βββ main/
β β βββ java/
β β β βββ com/
β β β βββ yourdomain/
β β β βββ myproject/
β β β βββ MyProjectApplication.java
β β βββ resources/
β β βββ application.properties
β β βββ static/
β β βββ templates/
β β βββ META-INF/
β βββ test/
β βββ java/
β βββ com/
β βββ yourdomain/
β βββ myproject/
β βββ MyProjectApplicationTests.java
π¦ File & Folder Breakdown
1. pom.xml (or build.gradle)
Defines your project’s:
- Dependencies
- Plugins
- Build instructions
Itβs the heart of your projectβs setup if using Maven.
2. src/main/java
This is where all your application code lives.
Follow this structure:
com.yourdomain.myproject
βββ controller/
βββ service/
βββ repository/
βββ model/
βββ MyProjectApplication.java
| Folder | Purpose |
|---|---|
controller/ | REST Controllers, API endpoints |
service/ | Business logic layer |
repository/ | Spring Data JPA repositories |
model/ | Entity or DTO classes |
| Main class | Starts the app (annotated with @SpringBootApplication) |
3. src/main/resources
Contains non-Java resources:
| File/Folder | Purpose |
|---|---|
application.properties or .yml | Configuration settings |
static/ | Static content (CSS, JS, images) |
templates/ | Thymeleaf or FreeMarker HTML templates |
META-INF/ | Metadata, such as MANIFEST.MF |
4. src/test/java
This is your test codebase.
Follow the same package structure as main/java for:
- Unit tests
- Integration tests
- Mock-based testing
Spring Boot uses JUnit 5 by default.
π§° Best Practices for Spring Boot Structure
β 1. Use Layered Architecture
Organize your code by function:
- Controller β Service β Repository
- Avoid putting everything in the controller
β 2. Group Features (Modular Structure)
For large apps, go feature-first instead of layer-first:
product/
βββ ProductController.java
βββ ProductService.java
βββ ProductRepository.java
βββ Product.java
This improves modularity and testability.
β 3. Use DTOs (Don’t Expose Entities Directly)
Map entity objects to Data Transfer Objects (DTOs) to control API response shape.
β
4. Keep application.properties Organized
Use:
spring.datasource.url=
spring.datasource.username=
app.feature.toggle.enabled=true
Use profiles like application-dev.properties, application-prod.yml for environment separation.
π¨ Common Mistakes to Avoid
| Mistake | Why itβs bad |
|---|---|
| All logic in controller | Violates separation of concerns |
| No structure (all classes in root) | Becomes unmanageable as app grows |
| Skipping test packages | Leads to hard-to-maintain code |
| Using default package | Causes scanning issues, bad practice |
π― Summary
A standard Spring Boot project structure looks like this:
src/
βββ main/java # Your code
βββ main/resources # Configs, templates, static files
βββ test/java # Your test code
With:
- Controllers handling input/output
- Services handling business logic
- Repositories handling data access
- Models representing data objects
Following these best practices ensures your codebase stays clean, modular, and scalable β even as your app grows.