Quick revision
The short answer
I would make controllers handle only HTTP concerns: mapping, validation, status codes and conversion between DTOs and calls. Business rules and transactions move into a service layer that can be tested without HTTP, and responses become DTOs instead of entities, which avoids lazy loading errors, JSON recursion and leaking fields. I would make the change step by step, starting with the endpoints that change most, rather than rewriting everything at once.
What to remember
In this answer, we took a controller that called repositories, applied business rules and returned entities, and saw the concrete costs: rules testable only through HTTP, no clear transaction, lazy loading errors and recursion in JSON, leaked fields and no reuse. We moved the rule into the domain and the use case into a transactional service, returned DTOs mapped inside the transaction, kept the controller to HTTP concerns, and rolled the change out step by step starting with the endpoints that change most.
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.
I would make controllers handle only HTTP concerns: mapping, validation, status codes and conversion between DTOs and calls. Business rules and transactions move into a service layer that can be tested without HTTP, and responses become DTOs instead of entities, which avoids lazy loading errors, JSON recursion and leaking fields. I would make the change step by step, starting with the endpoints that change most, rather than rewriting everything at once.
Why this question is asked
Fat controllers are one of the most common things we meet in code review, especially in services that grew quickly. They work, so it can be hard to explain why they should change. Interviewers ask this to see whether we can name the concrete problems, not just quote layering rules, and whether we can improve the code without stopping feature work. In this answer, we will look at a typical fat controller, name the problems it causes, refactor it into a thin controller with a service and DTOs, and plan how to roll the change out.
1What a Fat Controller Looks Like
Here is a typical example. The controller calls the repository, applies a discount rule, saves, and returns the entity.
2The Problems It Causes
The first problem is testing. The discount rule can only be tested through HTTP, with a web context and a database. A unit test that says VIP customers over 100 get 10 percent should run in milliseconds, but here it cannot exist.
The second is transactions. There is no @Transactional, so the two reads and the save each run in their own transaction. Another request can change the order between the read and the save. Putting @Transactional on the controller would work, but then the transaction also covers JSON serialisation and lazy loading during it, which is the wrong boundary.
The third is the response. Returning the entity means Jackson serialises every getter. A lazy collection outside a session throws LazyInitializationException, or with open-in-view it quietly runs extra queries. A bidirectional relation between Order and Customer can recurse until the stack overflows. And any field added to the entity later, such as an internal margin, is suddenly public.
The fourth is reuse. When the same confirmation is needed from a Kafka listener or a scheduled job, the logic is stuck in a class that only speaks HTTP, so it gets copied.
3Refactoring Into a Thin Controller and a Service
We move the rule and the transaction into an application service, and the controller only translates between HTTP and that service.
- ControllerPaths, validation, status codes, DTO in and out
- Application serviceUse case, transaction, permissions
- Domain modelBusiness rules such as the discount
- RepositoriesLoading and saving data
4 layers, from the top down. Step through them one at a time.
Each layer can be tested on its own; only the top one knows about HTTP.
Notice what changed. The transaction now wraps exactly the reads and the update, and the dirty-checked order is flushed at commit without an explicit save. The DTO is built inside the transaction, so every field it needs is loaded while the session is open, and nothing lazy reaches Jackson. The rule is a plain method we can unit test, and the Kafka listener can call the same service. The controller uses constructor injection, which also makes it easy to test with a mock.
4Rolling It Out Without a Big Rewrite
In a real codebase there may be fifty such controllers. Rewriting them all at once is risky and blocks feature work, so I would agree on the target shape with the team and then apply it gradually. The endpoints that change most often come first, because every future change there gets cheaper. The git history shows which ones those are. Endpoints that nobody has touched for two years can wait.
Each refactoring keeps the same API contract, so it is safe to ship on its own. A contract or MockMvc test that captures the current response shape before the change tells us if we broke anything. Once a few services exist, an ArchUnit rule can stop new code from regressing, for example by failing the build if a class annotated with @RestController depends on a repository directly or returns a type annotated with @Entity.
In the review itself, I would explain these reasons in the comments rather than just writing 'move this to a service', and offer to pair on the first one, because the goal is that the team writes it this way next time without being asked.
5Follow-up questions
Is a service layer just a pass-through in simple CRUD apps?
Sometimes it is thin, and that is fine. It still gives a transaction boundary and a place where a rule can go later. For truly trivial read endpoints, some teams let the controller call a read-only query service directly; what matters is that writes and rules do not live in controllers.
Where should mapping between DTOs and entities happen?
Inside the service or just at its edge, while the transaction is open, so lazy data is still loadable and the controller only sees DTOs. A mapper class or MapStruct can keep that code out of the service methods themselves.
6Conclusion
In this answer, we took a controller that called repositories, applied business rules and returned entities, and saw the concrete costs: rules testable only through HTTP, no clear transaction, lazy loading errors and recursion in JSON, leaked fields and no reuse. We moved the rule into the domain and the use case into a transactional service, returned DTOs mapped inside the transaction, kept the controller to HTTP concerns, and rolled the change out step by step starting with the endpoints that change most.
Open with
I'd push for controllers that only handle HTTP: the path, validation, status codes and turning the request into a call. Right now the business rule, like the VIP discount, can only be tested through HTTP and a database, so I'd move it into the domain or an application service where it gets a plain unit test.
Then, if they want more
The service also becomes the transaction boundary. At the moment the reads and the save run in separate transactions, and putting @Transactional on the controller would be the wrong place because it would also cover JSON serialisation. Then I'd replace the entity return values with DTOs mapped inside the transaction.
Returning entities causes LazyInitializationException or hidden queries for lazy relations, recursion between bidirectional associations, and it leaks any field we add to the entity later. Another benefit is reuse: a Kafka listener or a batch job can call the same service. I wouldn't ask for a big rewrite, though.
I'd agree on the target shape with the team, start with the endpoints that change most often, keep the API contract the same with a test around each one, and add an ArchUnit rule so new controllers don't depend on repositories or return entities.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
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 "In code review you see controllers that call repositories directly, return JPA entities...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/d7f11d32ba/