Quick revision
The short answer
A layered Spring application has controllers that handle HTTP, services that hold the use case logic and own the transaction boundaries, and repositories that talk to the database. The layers stay healthy when dependencies point only downward, when controllers return DTOs rather than JPA entities, and when architecture tests such as ArchUnit layer rules fail the build as soon as someone breaks the structure.
What to remember
In this answer, we saw that a layered Spring application splits work into controllers for HTTP and DTO mapping, services for use cases and transactions, and repositories for persistence. The structure stays healthy when dependencies point only downward, when JPA entities never leave through the controller, and when ArchUnit layer rules turn those agreements into tests that fail the build.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A layered Spring application has controllers that handle HTTP, services that hold the use case logic and own the transaction boundaries, and repositories that talk to the database. The layers stay healthy when dependencies point only downward, when controllers return DTOs rather than JPA entities, and when architecture tests such as ArchUnit layer rules fail the build as soon as someone breaks the structure.
Why this question is asked
Almost every Spring Boot service we join starts out layered, and most of them drift. A controller starts calling a repository directly to save time, a service begins returning ResponseEntity, an entity gets sent straight to the browser, and a year later nobody is sure where a rule lives. Interviewers ask this question to see whether we know what each layer is for and, more importantly, which rules stop the structure from eroding. In this answer, we will recap the three layers, follow one request through them, look at the rule that dependencies point downward, see why entities must not leave the controller, and finish with ArchUnit tests that keep the structure honest.
1A Quick Recap of the Three Layers
Layered architecture splits an application by technical responsibility. Each layer has one job and talks only to the layer below it.
The controller layer is the web edge. A controller, a class annotated with @RestController, receives an HTTP request, parses the path and body, validates the input, maps it to whatever the service expects, calls the service, and maps the result back to a response DTO, a data transfer object that exists only to shape the JSON. It should know about HTTP and nothing about SQL.
The service layer holds the use case: the steps and business rules of placing an order or cancelling a subscription. Services are also where transaction boundaries belong, so @Transactional goes on service methods. The reason is that one use case may touch several repositories, and all of those changes must commit or roll back together.
The repository layer handles persistence. In Spring Data JPA, a repository is often just an interface extending JpaRepository, and Spring generates the implementation. It knows how to load and save entities and run queries, and nothing about HTTP or business workflows.
- ControllerHTTP, validation, DTO mapping
- Serviceuse case logic,
@Transactional - Repositoryload and save entities, queries
- Databasetables owned by this service
4 layers, from the top down. Step through them one at a time.
Each layer calls only the one below it.
2Following One Request Through the Layers
Here is a small order endpoint written the healthy way. Notice what each class imports and what it returns.
The controller deals with @Valid, status codes and JSON shapes. The service has no idea it was called over HTTP; it could be called from a message listener tomorrow without change. The repository is plain persistence. So when a rule about orders changes, we know to look in the service or the domain object, and when the JSON changes, we know to look in the controller and its DTOs.
3Rule 1: Dependencies Point Downward Only
The most important rule is the direction of calls. Controllers call services, services call repositories, and never the reverse. A repository never calls a service, and a service never calls a controller or uses web types such as HttpServletRequest or ResponseEntity.
The reason is change isolation. If a service depends on web types, it can no longer be reused from a batch job or a Kafka listener, and it cannot be tested without a web context. If a controller skips the service and calls the repository directly, the business rules and the transaction boundary are bypassed, and the next person who adds a rule to the service is surprised that one endpoint ignores it.
There is one more quiet consequence: circular dependencies. When layers call upward, Spring may report a cycle between beans at startup, and even when it does not, the code becomes hard to reason about because there is no longer a clear top and bottom.
4Rule 2: Do Not Return JPA Entities From Controllers
It is tempting to return the Order entity directly from the controller, because it already has all the fields. This is where many layered applications break down.
An entity is a persistence object: it mirrors the table and is managed by Hibernate. When Jackson, the JSON library Spring Boot uses, serialises it, it calls every getter. A lazy association, one Hibernate loads only when first accessed, will either run an extra SQL query for each order, which gives the N+1 problem, or throw LazyInitializationException if the transaction has already ended. Internal fields such as audit columns or a passwordHash appear in the API. Bidirectional relationships can produce endless JSON cycles. And the API contract is now tied to the database schema, so renaming a column breaks clients.
With a DTO, the API shows exactly what we chose, and the mapping happens where we can control which associations are loaded.
5Keeping the Structure From Eroding With ArchUnit
Rules written on a wiki are forgotten within months. ArchUnit is a library that lets us write architecture rules as ordinary JUnit tests, which run on every build. If someone adds a controller that injects a repository, the build fails with a message naming the class.
Because the rule is code, it travels with the project, it is reviewed like code, and new team members learn the structure from a failing test rather than from a review comment.
6Follow-up questions
Should services return entities or DTOs?
Within the application, services may return domain objects or entities, and the controller maps them to response DTOs. Some teams prefer services to return DTOs or read models so the controller never touches an entity. Both work; what matters is that the mapping happens while the needed data is loaded and that entities never reach the JSON serialiser.
Where does validation belong?
Format validation, such as a required field or a maximum length, belongs on the request DTO with Bean Validation annotations and @Valid in the controller. Business validation, such as whether a customer may order this product, belongs in the service or the domain object, because it must apply however the use case is called.
Can a service call another service?
Yes, within the same layer that is normal, but watch for cycles and for one service reaching into another's repository. If two services call each other, there is usually a missing third class or a feature boundary in the wrong place.
7Conclusion
In this answer, we saw that a layered Spring application splits work into controllers for HTTP and DTO mapping, services for use cases and transactions, and repositories for persistence. The structure stays healthy when dependencies point only downward, when JPA entities never leave through the controller, and when ArchUnit layer rules turn those agreements into tests that fail the build.
Open with
A typical layered Spring service has three layers. Controllers handle HTTP: they parse the request, validate it with Bean Validation, map it to what the service needs, and map the result back to a response DTO.
Then, if they want more
Services hold the use case logic, and that is where I put @Transactional, because one use case can touch several repositories and they all need to commit or roll back together. Repositories, usually Spring Data interfaces, handle persistence and nothing else. The rules I care about most are these.
Dependencies point downward only: controllers call services, services call repositories, and never the other way, so services never use web types like ResponseEntity and controllers never skip the service to call a repository.
I never return JPA entities from controllers, because Jackson then touches lazy associations, which gives N+1 queries or LazyInitializationException, and it leaks internal fields and ties the API to the schema; I return DTOs or records instead.
And because written rules erode, I add ArchUnit tests with a layeredArchitecture rule, so a controller that injects a repository fails the build with a clear message instead of slipping through review.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe a typical layered Spring application with controller, service and repository...": https://scrivio-interview-library.netlify.app/java/application-architecture/087d24cb2d/