How an HTTP request becomes a controller method call — and how the return value becomes a response.
DispatcherServlet as the front controller and the request flow; @Controller vs
@RestController; the parameter annotations (@PathVariable, @RequestParam,
@RequestBody) and when each applies; and how a returned object becomes JSON.
Every request enters through one servlet: the DispatcherServlet. It doesn't do
the work itself — it orchestrates helpers to route the request, invoke your controller, and render the response.
Spring Boot auto-configures it for you.
DispatcherServletHandlerMapping finds the controller method that matches the URL + HTTP methodHandlerAdapter invokes that method, binding its arguments from the requestHttpMessageConverter serialises the object to JSON. MVC: a ViewResolver + View render HTMLspring-boot-starter-web brings in Spring MVC, an embedded Tomcat, and Jackson (for JSON),
and auto-configures the DispatcherServlet and message converters. You just write controllers.
@Controller | @RestController | |
|---|---|---|
| Equals | a web stereotype | @Controller + @ResponseBody |
A String return means | a view name (resolved by a ViewResolver) | the response body itself |
| Typical use | server-rendered HTML pages | REST/JSON APIs |
@RestController
@RequestMapping("/api/accounts") // class-level base path for every handler
public class AccountController {
@GetMapping("/{id}") // GET /api/accounts/42
public Account get(@PathVariable Long id) { ... }
@GetMapping // GET /api/accounts?active=true
public List<Account> list(@RequestParam boolean active) { ... }
@PostMapping // POST body → Account
public Account create(@RequestBody Account a) { ... }
}
@RequestMapping maps requests to handlers; the HTTP-verb shortcuts are
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping,
@PatchMapping. On a class, @RequestMapping sets a base path prefixed to every method.
| Annotation | Binds from | Example |
|---|---|---|
@PathVariable | a URI template variable | /users/{id} → id |
@RequestParam | a query-string / form parameter | ?active=true → active |
@RequestBody | the request body (deserialised via a converter) | JSON → an object |
@RequestHeader | a request header | @RequestHeader("User-Agent") |
Controllers can also take Model, HttpServletRequest/Response,
Principal, and more as parameters.
@RequestParam vs @PathVariable
@PathVariable pulls a value out of the path itself (/users/42);
@RequestParam reads a query-string value (/users?id=42). Mixing them up is a
guaranteed lost mark.
For server-rendered HTML (plain @Controller), a returned view name is resolved to a template.
Spring supports Thymeleaf, FreeMarker, Groovy Markup, and
Mustache (JSP too, in traditional deployments). For REST you skip views entirely — the
HttpMessageConverter writes JSON.
Read "DispatcherServlet", "Annotated Controllers", and "Handler Methods" (the argument/return-value tables).
The parameter-binding annotations and the DispatcherServlet flow are the money questions. Options shuffle on every load.
RestTemplate.