Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

REST controllers in your company often contain logic that should belong to the domain, making it hard to add other channels like messaging or GraphQL. How would you change the architecture over the coming years?

I would move domain logic out of controllers into application services that know nothing about HTTP, so controllers, message listeners and GraphQL resolvers become thin adapters over the same use cases. The change happens when teams touch the code, not as a big rewrite, guarded by architecture tests such as ArchUnit rules so the boundary holds, and tracked with a simple measure of how much logic still lives in controllers.

Why this question is asked

When business rules live in REST controllers, every new channel copies them. The first Kafka consumer reimplements the order validation slightly differently, the GraphQL resolver gets a third version, and soon the rules disagree. At a senior level, interviewers want a plan that fixes the architecture over years without freezing feature work. In this answer, we will see why logic ends up in controllers, define the target shape with thin adapters over application services, plan the migration as part of normal work, protect the boundary with tests, and measure progress.

1Why Logic Ends Up in Controllers

It usually starts innocently. The first version of a service has one channel, HTTP, and the controller is the most obvious place to put code. A check here, a calculation there, and soon the controller knows how orders are priced, which statuses can follow which, and who may cancel. Nobody decided this; it is just the path of least resistance when there is only one entry point.

The cost appears when a second entry point arrives. A message from another system should place an order too, or the mobile team wants a GraphQL API. Calling the controller from a listener is awkward because it expects HTTP types and returns ResponseEntity, so people copy the logic. Copies drift, and bugs appear where two channels behave differently for the same input.

2The Target Shape: Thin Adapters Over Application Services

The target is an application service per use case, for example PlaceOrder or CancelOrder, that takes plain input types, enforces business rules through the domain model, owns the transaction, and returns plain results. It does not import anything from Spring MVC, messaging or GraphQL. Every channel is then an adapter that translates its own input into a call, and the result back into its own output. This is the idea behind hexagonal or ports-and-adapters architecture, but we do not need the vocabulary to apply it.

Java
public record PlaceOrderCommand(String customerId, List<Line> lines) {}
@Service
public class PlaceOrder { // no HTTP, no Kafka types
@Transactional
public OrderPlaced handle(PlaceOrderCommand cmd) { ... } // rules, persistence, events
}
@RestController
class OrderHttpAdapter {
@PostMapping("/orders")
ResponseEntity<OrderDto> place(@Valid @RequestBody PlaceOrderRequest req) {
OrderPlaced r = placeOrder.handle(req.toCommand());
return ResponseEntity.created(uriFor(r.id())).body(OrderDto.from(r));
}
}
@Component
class OrderKafkaAdapter {
@KafkaListener(topics = "partner-orders")
void on(PartnerOrderMessage m) { placeOrder.handle(m.toCommand()); } // same rules as HTTP
}
@Controller
class OrderGraphQlAdapter {
@MutationMapping
OrderView placeOrder(@Argument PlaceOrderInput input) {
return OrderView.from(placeOrder.handle(input.toCommand()));
}
}
Step throughChannels as adapters over one set of use cases
  1. HTTP, Kafka, GraphQL, batch adaptersTranslate input and output; no business rules
  2. Application services (use cases)PlaceOrder, CancelOrder; transactions and permissions
  3. Domain modelOrder, pricing, allowed status changes
  4. InfrastructureRepositories, outgoing clients, event publishing

4 layers, from the top down. Step through them one at a time.

Notice that the three adapters are each a few lines, and none of them contains a rule. Adding a fourth channel next year, for example a batch import, means one more adapter, not a fourth copy of the logic.

3Migrate When Touching the Code

Stopping feature work for a year-long refactoring rarely gets approved and rarely finishes. Instead, the rule for teams is simple: when you change an endpoint, or when you need its logic in a second channel, first extract the logic into an application service, then make your change there. Code nobody touches can stay as it is; it is not hurting anyone.

To make this realistic, the extraction must be cheap and safe. Before moving code, the team adds a test around the endpoint's current behaviour, a MockMvc or contract test, then moves the logic, and the test confirms nothing changed. Templates and a short guide with one worked example show what the target looks like in our codebase, and pairing on the first few extractions spreads the habit. Planning should allow for it, perhaps 10 to 20 percent extra on stories that touch fat controllers.

4Keep the Boundary With Architecture Tests

Once a few services are clean, we need to keep them clean. ArchUnit, a library that checks dependency rules as ordinary unit tests, can fail the build when a boundary is crossed.

Java
@AnalyzeClasses(packages = "com.shop")
class ArchitectureTest {
@ArchTest
static final ArchRule servicesDoNotKnowHttp =
noClasses().that().resideInAPackage("..application..")
.should().dependOnClassesThat().resideInAnyPackage(
"org.springframework.web..", "jakarta.servlet..", "org.springframework.kafka..");
@ArchTest
static final ArchRule controllersDoNotUseRepositories =
noClasses().that().areAnnotatedWith(RestController.class)
.should().dependOnClassesThat().areAssignableTo(Repository.class);
}
// Build fails: "Class OrderController depends on OrderRepository in (OrderController.java:23)"

For services that are still messy, rules can be frozen: ArchUnit's FreezingArchRule records the existing violations and fails only on new ones. That lets us introduce the rule on day one without fixing everything first, and the stored list of violations shrinks as teams extract logic.

5Measure Progress Over the Years

A multi-year change needs a number people can see. Good candidates are the count of frozen ArchUnit violations per service, the number of controllers that depend on repositories, or the average size of controller methods. None is perfect, but each trends in the right direction as logic moves out. A quarterly look at the numbers per team, next to the list of channels each service now supports, shows whether the work is paying off.

It also helps to celebrate the payoff explicitly: the first time a team adds a Kafka or GraphQL channel in a day because the use case already existed, that story is worth more for adoption than any guideline.

6Follow-up questions

Is this the same as hexagonal architecture?

It is the same core idea: business logic in the middle, adapters at the edges, dependencies pointing inwards. We do not need every element of a formal hexagonal design, such as a port interface for every repository, to get the main benefit. Starting with application services and thin adapters gives most of the value with little ceremony.

What about validation? Does it stay in the controller?

Format validation of the channel's input, such as a required field in a JSON body, stays in the adapter with Bean Validation. Business validation, such as whether this customer may order this product, belongs in the domain or service, so every channel applies it the same way.

7Conclusion

In this answer, we saw that logic drifts into REST controllers because HTTP is often the only channel at first, and that it gets copied and diverges when messaging or GraphQL arrive. The target is application services independent of HTTP, with every channel as a thin adapter. We migrate when touching code, after adding a test around current behaviour, protect the boundary with ArchUnit rules frozen at today's violations, and measure progress by the logic left in controllers.

What to say in the interviewAbout 90 seconds aloud

Open with

The root problem is that our controllers are the only place the business rules exist, so every new channel either calls awkwardly into HTTP code or copies the logic, and copies drift.

Then, if they want more

The target I'd set is an application service per use case, like PlaceOrder or CancelOrder, that takes plain commands, enforces the rules through the domain model, owns the transaction and has no HTTP or messaging types in it.

Then REST controllers, Kafka listeners and GraphQL resolvers are all thin adapters, a few lines each, translating their input into a command and the result back. I wouldn't do it as a big rewrite.

The rule would be that when a team changes an endpoint or needs its logic in another channel, they first extract it into a service, with a test around the current behaviour so nothing changes by accident, and we budget some extra time on those stories.

To keep the boundary, I'd add ArchUnit tests, for example that application packages don't depend on Spring Web or Kafka and controllers don't use repositories, and freeze existing violations so only new ones fail the build.

Progress is measured by the shrinking number of violations and how much logic is left in controllers, and I'd share the wins when a new channel takes a day instead of weeks.

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.
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 "REST controllers in your company often contain logic that should belong to the domain...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/8a38b55246/