controller (Spring)
A Spring class, usually marked @RestController, whose methods handle HTTP requests for particular paths and return response bodies.
In more detail
A controller is the boundary between HTTP and application code: it reads transport data, calls a service and chooses a status and response model. Business decisions belong in the service so they can be reused and tested without a web server. Keep controllers thin enough that the request and response contract is visible in one place.
Example
@RestController public class OrderController { @GetMapping("/orders") String list() { ... } }Related terms
- request mapping — The annotations, such as @GetMapping("/orders/{id}"), that connect an HTTP method and path to a controller method.
- Spring MVC — Spring's framework for web applications and APIs, which routes each HTTP request to a controller method and turns its return value into a response.
- service layer — The layer of an application that holds use cases and business rules, sitting between controllers and repositories.
- controller advice — A Spring class, marked @RestControllerAdvice, whose exception handlers apply across all controllers, giving one failure policy for every route.
- layered architecture — Organising a system into ranked layers where each layer may only use layers below it.
Taught in
- Controllers And Routing — Modern Java: Virtual Threads & High-Throughput Services
- Reading The Request — Modern Java: Virtual Threads & High-Throughput Services
- Implementing An OpenAPI Contract — Modern Java: Virtual Threads & High-Throughput Services
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services
- Tying It All Together — Modern Java: Virtual Threads & High-Throughput Services