Spring Boot is one of the most popular Java frameworks for building scalable web applications. But writing good code isn't enoughβstructuring your project properly is equally crucial. The right folder/package structure:
- Enhances code readability and maintainability
- Helps teams scale and collaborate
- Aligns with domain modeling and microservices architecture
In this blog, weβll explore the most commonly adopted folder structures across the industry, with examples, pros & cons, and when to use what.
π§± 1. Standard Layered Architecture
π€ Best For: Monoliths, small-to-mid web apps
This is the most traditional structure used by most teams starting with Spring Boot.
com.example.myapp
βββ controller // REST Controllers
βββ service // Business logic
βββ repository // Spring Data Repositories
βββ model // JPA Entities or POJOs
βββ dto // Request/Response DTOs
βββ config // Spring Boot Configs
βββ exception // Custom exceptions, handlers
βββ mapper // MapStruct or manual mappers
βββ MyAppApplication.java
β Pros
- Easy to learn and organize
- Aligned with typical 3-tier architecture
β Cons
- Can get bloated in large projects
- Tightly couples layers
π§ Example
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
@GetMapping("/{id}")
public UserDto getUser(@PathVariable Long id) {
return userService.getUserById(id);
}
}
π§© 2. Feature-Based Modular Structure (Vertical Slice)
π Best For: Medium to large modular apps
Organized by feature instead of layers. Ideal when teams work on distinct modules.
com.example.myapp
βββ features
β βββ user
β β βββ controller
β β βββ service
β β βββ repository
β β βββ dto
β β βββ model
β β βββ mapper
β βββ product
β βββ controller
β βββ service
β βββ repository
β βββ dto
βββ config
βββ exception
βββ MyAppApplication.java
β Pros
- Scales well with multiple teams
- Each feature is self-contained
β Cons
- Slight learning curve
- Shared logic must be carefully managed
π§± 3. Hexagonal Architecture (Ports & Adapters / Clean Architecture)
ποΈ Best For: Domain-Driven, long-lived enterprise systems
Separates domain, application logic, and infrastructure clearly.
com.example.myapp
βββ domain
β βββ model
β βββ service
βββ application
β βββ usecases
βββ infrastructure
β βββ persistence
β βββ rest
βββ web
β βββ controller
βββ config
βββ MyAppApplication.java
β Pros
- Testable and decoupled
- Clean separation of concerns
β Cons
- More boilerplate and upfront design
- Not suited for trivial apps
π Example:
public interface UserRepository {
Optional<User> findById(Long id);
}
Implemented by an adapter in infrastructure.persistence
.
π§³ 4. Multi-Module Maven Structure
𧬠Best For: Microservices, shared modules
Each feature is an independent module, often maintained in different repos.
project-root/
βββ common/ // Shared DTOs, utils
β βββ src/main/java/...
βββ user-service/
β βββ src/main/java/com/example/user
βββ product-service/
β βββ src/main/java/com/example/product
βββ pom.xml // Parent POM
β Pros
- Clear modular boundaries
- Reusable components
β Cons
- Maven complexity
- Shared versioning can be tricky
π 5. Reactive Structure (WebFlux / Handler-Based)
βοΈ Best For: Reactive, async applications
Uses functional routing over MVC annotations.
com.example.myapp
βββ handler // Functional endpoint handlers
βββ router // RouterFunction configs
βββ service
βββ repository
βββ model
βββ MyAppApplication.java
Example Router
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions
.route(GET("/users/{id}"), handler::getUser);
}
β Pros
- Non-blocking, high throughput
- Lightweight functional style
β Cons
- Functional style is less familiar
- Debugging stack traces is harder
βοΈ 6. CQRS-Oriented Structure
π§ Best For: High-scale systems, Event Sourcing
Separates read and write logic.
com.example.myapp
βββ command
β βββ handler
β βββ service
β βββ controller
βββ query
β βββ service
β βββ controller
βββ events
β βββ listeners
βββ MyAppApplication.java
β Pros
- Optimized for scale
- Clear boundaries
β Cons
- Higher complexity
- Eventual consistency challenges
πΈοΈ 7. GraphQL-Driven Structure
π Best For: GraphQL-first APIs
com.example.myapp
βββ graphql
β βββ resolver
β βββ schema
β βββ dto
β βββ service
βββ repository
βββ MyAppApplication.java
β Pros
- Logical schema-based grouping
- Supports GraphQL-first development
β Cons
- Requires GraphQL learning
- Less RESTful structure
π Comparison Table
Structure Type | Use Case | Pros | Cons |
---|---|---|---|
Layered | Monoliths | Easy, traditional | Bloated in large apps |
Feature-Based | Modular, team-split projects | Scalable, modular | Shared code management |
Hexagonal | Clean DDD applications | Decoupled, testable | Design-heavy |
Multi-Module | Microservices, shared libs | Isolated, reusable | Maven/Gradle complexity |
Reactive | WebFlux-based async apps | Lightweight, fast | Less IDE-friendly |
CQRS | Event-based systems | High throughput | Complex to maintain |
GraphQL | GraphQL-first BFF or APIs | Schema-aligned | Requires schema tooling |
π οΈ Which Structure Should You Use?
Your Need | Recommended Structure |
---|---|
Building a quick CRUD API | Layered |
Multi-team, large-scale product | Feature-based |
Long-term investment, strong domain logic | Hexagonal / Clean |
Need shared libraries or services | Multi-module |
Reactive WebSockets / streaming | Reactive |
Split reads and writes cleanly | CQRS |
GraphQL-first application | GraphQL-based |
π Final Thoughts
Thereβs no one-size-fits-all. The right structure depends on your:
- Team size
- Codebase complexity
- Domain modeling
- Delivery expectations
Start simple, and evolve your structure as the system grows.
π§° Bonus: Generate Feature Skeleton with Spring Boot CLI
spring init --dependencies=web,data-jpa --build=maven --package-name=com.example.user user-service
Want a GitHub Starter Template?
π Drop a comment or message, and Iβll share a starter repo matching your preferred structureβlayered, modular, reactive, or clean architecture.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.