Spring Boot Architecture
A maintainable Spring Boot service is layered: the web layer speaks HTTP, the service layer owns business rules and transactions, the persistence layer talks to the database, an…
Introduction
A maintainable Spring Boot service is layered: the web layer speaks HTTP, the service layer owns business rules and transactions, the persistence layer talks to the database, and configuration wires infrastructure beans.
DispatcherServlet is the front controller for servlet MVC. It routes to a @RestController method, which should validate input, call a service, and return a DTO or record — not open a DataSource.
When Orders grows, the temptation is to put pricing, inventory, and persistence in the controller. That creates untestable HTTP-bound logic and forces every change through a web test. Layers exist so you can unit-test OrderService.place with fakes.
This lesson is about boundaries, not folders. You may package by layer early and by feature later; the call direction stays the same: controller → service → repository.
Purpose of this lesson
Draw the Orders request path through Controller → Service → Repository and state what each layer may and may not depend on.
Understanding the topic
Dependency rules beat package names.
- Controller: HTTP mapping, status codes, DTO/record mapping. No SQL. No multi-step business workflows.
- Service: use cases, invariants,
@Transactionalboundaries. Depends on ports/repositories, not onHttpServletRequest. - Repository: persistence only. Spring Data interfaces or
JdbcClient. No authorization decisions. - Domain: entities/records that model Orders. Keep HTTP annotations off domain types.
- Config:
@BeanforRestClient,Clock, data sources — not business rules.
Step-by-step explanation
- Sketch
GET /api/orders/{id}from client to database on paper. - Name the types:
OrderController,OrderService,OrderRepository,OrderView. - Forbid repository injection into the controller in code review.
- When a feature needs two repositories, orchestrate in the service — not in the controller.
Syntax reference
Happy-path request flow:
HTTP GET /api/orders/42│▼DispatcherServlet│▼OrderController.getOne(id) // map HTTP ↔ OrderView│▼OrderService.get(id) // rules, @Transactional if writes│▼OrderRepository // JPA / JDBC│▼PostgreSQL
Informative example
Thin controller, injectable service. The controller does not know how pricing works.
@RestController@RequestMapping("/api/orders")public class OrderController {private final OrderService orders;public OrderController(OrderService orders) { this.orders = orders; }@GetMapping("/{id}")OrderView getOne(@PathVariable long id) {return orders.get(id);}}
Real-world use
Payment platforms model a transfer as controller (authz + DTO) → service (idempotency, ledger postings, fraud checks) → repositories (accounts, ledger lines). TechLearningPRO Orders follows the same seam so checkout rules can be tested without MockMvc for every assertion.
Best practices
- DTOs/records at the HTTP boundary; entities inside the service/persistence edge.
- One public use-case method on the service per business action when possible.
- Keep controllers short enough to read without scrolling.
Common mistakes
- Injecting
OrderRepositoryintoOrderController. - Returning JPA entities from controllers (lazy loads, accidental over-exposure).
- Putting
@Transactionalonly on the controller — web concerns and persistence lifecycle get tangled.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy shouldn't a controller call a repository directly?+
Answer
2IntermediateQuestionWhat is the role of DispatcherServlet?+
Answer
3AdvancedQuestionWhen is package-by-feature better than package-by-layer?+
Answer
Hands-on exercise
Lab. List three Orders features (get, place, cancel). For each, write which layer owns the work and which types are forbidden to import in the controller.
Summary
Orders stays healthy when each layer has one job. The next lessons install the toolchain and scaffold the project that will hold these layers.