Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A use case class PlaceOrderUseCase imports org.springframework.web.client.RestTemplate to call the inventory service and returns ResponseEntity<OrderDto>. Which architectural rule is broken, and how do you fix it?

The use case breaks the dependency rule: it depends on outer, web framework types, RestTemplate for the outgoing call and ResponseEntity for the result, so its dependencies point outward and business logic is tied to HTTP. We fix it by defining an InventoryPort interface in the application layer, implementing it in an adapter that uses an HTTP client, and returning a domain result from the use case that the controller maps to an HTTP response. The use case can then be tested without HTTP or Spring.

Why this question is asked

This is one of the most common ways a clean or hexagonal codebase erodes. Someone needs to call another service from a use case, the HTTP client is right there, and returning ResponseEntity seems convenient because the controller wants one anyway. Each choice is small, but together they tie the business logic to the web layer. Interviewers ask it because spotting the rule that is broken, and fixing it in the right direction, shows we understand why the structure exists. In this answer, we will recap the dependency rule, look at the broken code, explain what each import costs, fix it step by step with a port and an adapter, and see how testing becomes simple.

1A Quick Recap of the Dependency Rule

In clean architecture and hexagonal architecture, the code is arranged in rings. The inner rings hold the business: domain objects and use cases. The outer rings hold the technical details: web controllers, HTTP clients, databases and frameworks. The dependency rule says that source code dependencies point only inward. An inner class must not import, name or know about anything in an outer ring.

A use case is an application-layer class that carries out one business action, such as placing an order. It belongs to an inner ring. RestTemplate, Spring's HTTP client, and ResponseEntity, Spring's representation of an HTTP response, both belong to the outermost ring of frameworks and web delivery.

2The Broken Code

Here is roughly what the question describes.

Java
package com.acme.orders.application;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class PlaceOrderUseCase {
private final RestTemplate rest;
private final OrderRepository orders;
public ResponseEntity<OrderDto> place(PlaceOrderCommand cmd) {
StockResponse stock = rest.getForObject(
"http://inventory/stock/{sku}", StockResponse.class, cmd.sku()); // HTTP inside the use case
if (stock.available() < cmd.quantity()) {
return ResponseEntity.status(409).build(); // HTTP status as business result
}
Order order = orders.save(Order.place(cmd));
return ResponseEntity.ok(OrderDto.from(order));
}
}

Notice the two imports from org.springframework.web and org.springframework.http. The use case, an inner-ring class, now depends on the web framework. Its dependencies point outward.

3What Each Dependency Costs

The RestTemplate dependency means the use case knows that inventory is reached over HTTP, at which URL, with which response format. If inventory moves to gRPC, to a message-based reservation, or into the same process as a module, the business logic must change. Error handling also leaks: a timeout surfaces as a Spring exception in the middle of the use case.

The ResponseEntity return type is worse. It means the business result of placing an order is expressed as an HTTP status code. If we want to place orders from a Kafka listener or a batch import, there is no HTTP request to answer, yet the use case still returns 409 Conflict. The decision insufficient stock, which is a business fact, has been encoded as a transport detail.

And testing suffers: to test the stock rule we must mock RestTemplate and inspect HTTP statuses, which is slow to write and tests the wrong thing.

4Fix Step 1: An InventoryPort in the Application Layer

We start by asking what the use case really needs. It does not need HTTP; it needs to know how many items are available. So we define that need as an interface, a port, in the application layer, in the use case's own language.

Java
package com.acme.orders.application.port;
public interface InventoryPort {
int availableQuantity(Sku sku);
}
// Adapter, in an outer package
package com.acme.orders.adapter.inventory;
@Component
class HttpInventoryAdapter implements InventoryPort {
private final RestClient client; // RestClient is the newer synchronous client in Spring 6.1 and later
HttpInventoryAdapter(RestClient.Builder builder) { this.client = builder.baseUrl("http://inventory").build(); }
public int availableQuantity(Sku sku) {
StockResponse r = client.get().uri("/stock/{sku}", sku.value()).retrieve().body(StockResponse.class);
return r.available(); // translate their response into what the port promises
}
}

The interface lives in the inner ring, and the adapter that implements it lives in the outer ring. So the source code dependency now points from the adapter inward to the port. At runtime the call still goes from the use case to inventory, but the use case only knows the interface. This flip is called dependency inversion.

5Fix Step 2: Return a Domain Result

Next, the use case returns what happened in business terms, and the controller decides what that means in HTTP.

Java
public sealed interface PlaceOrderResult {
record Placed(OrderId id) implements PlaceOrderResult { }
record InsufficientStock(Sku sku, int available) implements PlaceOrderResult { }
}
public class PlaceOrderUseCase {
private final InventoryPort inventory;
private final OrderRepository orders;
public PlaceOrderUseCase(InventoryPort inventory, OrderRepository orders) { this.inventory = inventory; this.orders = orders; }
public PlaceOrderResult place(PlaceOrderCommand cmd) {
int available = inventory.availableQuantity(cmd.sku());
if (available < cmd.quantity()) return new PlaceOrderResult.InsufficientStock(cmd.sku(), available);
return new PlaceOrderResult.Placed(orders.save(Order.place(cmd)).id());
}
}
// Controller, in the web adapter
@PostMapping("/orders")
ResponseEntity<?> post(@RequestBody PlaceOrderRequest req) {
return switch (useCase.place(req.toCommand())) {
case PlaceOrderResult.Placed p -> ResponseEntity.status(201).body(new OrderCreated(p.id().value()));
case PlaceOrderResult.InsufficientStock s -> ResponseEntity.status(409).body(new Problem("insufficient stock"));
};
}
Step throughAfter the fix: who knows what
ControllerPlaceOrderUseCaseInventoryPort adapterInventory service
place(command)
availableQuantity(sku) through the port
GET /stock/{sku}
200 {available: 2}
2
InsufficientStock(sku, 2)
map to 409 Conflict

7 messages in order. Play them, or step through them one at a time.

Only the outer pieces know about HTTP; the use case speaks in business results.

The sealed interface lists every outcome, and the switch in the controller must handle each one, so adding an outcome later makes the compiler point at every place that needs updating. The HTTP status 409 is now chosen in the controller, where HTTP belongs.

6Testing the Use Case Without HTTP or Spring

Now the business rule can be tested with a lambda as the port, because InventoryPort has a single method.

Java
@Test
void refusesWhenStockIsLow() {
InventoryPort twoLeft = sku -> 2;
var useCase = new PlaceOrderUseCase(twoLeft, new InMemoryOrders());
var result = useCase.place(new PlaceOrderCommand(new Sku("A-1"), 5));
assertThat(result).isEqualTo(new PlaceOrderResult.InsufficientStock(new Sku("A-1"), 2)); // passes
}

The test runs in milliseconds and says exactly what the rule is. The HTTP adapter gets its own test, for example against WireMock, and the controller mapping gets a web slice test.

7Follow-up questions

Is RestTemplate itself the problem?

No. RestTemplate is a fine client, though RestClient is the newer recommended synchronous API. The problem is where it is used. Inside an adapter it is perfectly correct; inside a use case it ties business logic to HTTP.

How do you stop this happening again?

Add an ArchUnit rule that classes in the application package must not depend on org.springframework.web or org.springframework.http, or put the application layer in its own build module without those dependencies, so the import does not compile.

Where should the HTTP timeout and retry live?

In the adapter. The port can document that it may fail with a domain-level exception such as InventoryUnavailable, and the adapter translates timeouts and 5xx responses into it. The use case then decides the business response, for example refusing the order or accepting it for later confirmation.

8Conclusion

In this answer, we saw that PlaceOrderUseCase breaks the dependency rule because it imports RestTemplate and returns ResponseEntity, so business logic depends on web framework types and expresses its outcome as an HTTP status. We fixed it by defining an InventoryPort in the application layer with an HTTP adapter implementing it, and by returning a domain result that the controller maps to 201 or 409. The use case can now be tested with a lambda and no Spring at all.

What to say in the interviewAbout 90 seconds aloud

Open with

The broken rule is the dependency rule: source code dependencies should point inward, and a use case is an inner-ring class, but here it imports RestTemplate and returns ResponseEntity, which are both outer-ring web framework types.

Then, if they want more

That causes two problems. Using RestTemplate inside the use case means business logic knows that inventory is reached over HTTP, at which URL and in which format, so changing the transport means changing the use case.

Returning ResponseEntity means the business outcome, like not enough stock, is expressed as an HTTP 409, which makes no sense if the use case is called from a message listener or a batch job.

To fix it, I would define an InventoryPort interface in the application layer that says what the use case actually needs, for example availableQuantity of a SKU, and implement it in an adapter in the outer layer using RestClient or RestTemplate.

Then the use case returns a domain result, for example a sealed interface with Placed and InsufficientStock, and the controller maps those to 201 or 409.

After that, I can test the use case with a lambda as the port and an in-memory repository, no HTTP and no Spring, and I would add an ArchUnit rule so the application package cannot import Spring web types again.

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 "A use case class PlaceOrderUseCase imports org.springframework.web.client.RestTemplate...": https://scrivio-interview-library.netlify.app/java/application-architecture/77cb350ce9/