Lesson 11 · Spring MVC · starts Section 3

Spring MVC & the Request Lifecycle

How an HTTP request becomes a controller method call — and how the return value becomes a response.

What you'll be tested on The 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.

The DispatcherServlet — front controller

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.

  1. Request arrives at the DispatcherServlet
  2. HandlerMapping finds the controller method that matches the URL + HTTP method
  3. A HandlerAdapter invokes that method, binding its arguments from the request
  4. The method returns — a view name (MVC) or a value/object (REST)
  5. REST: an HttpMessageConverter serialises the object to JSON.  MVC: a ViewResolver + View render HTML
  6. The response is written back to the client

Boot MVC setup

spring-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.

Controllers

@Controller@RestController
Equalsa web stereotype@Controller + @ResponseBody
A String return meansa view name (resolved by a ViewResolver)the response body itself
Typical useserver-rendered HTML pagesREST/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.

Binding request data to parameters

AnnotationBinds fromExample
@PathVariablea URI template variable/users/{id}id
@RequestParama query-string / form parameter?active=trueactive
@RequestBodythe request body (deserialised via a converter)JSON → an object
@RequestHeadera 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.

Views & template engines

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.

Primary source — read this
Spring Framework 5.3 — Web MVC: DispatcherServlet & Annotated Controllers

Read "DispatcherServlet", "Annotated Controllers", and "Handler Methods" (the argument/return-value tables).

Check yourself

The parameter-binding annotations and the DispatcherServlet flow are the money questions. Options shuffle on every load.

Second pass The same ground is in your book's MVC section (13 questions). Then continue — REST builds on this with the HTTP verbs, status codes, and RestTemplate.
I'm your teacher — ask me anything. Want the full request-lifecycle traced through a real GET, or the difference between returning a view and returning JSON? Ask. Say "continue" for Lesson 12 — REST.
← Lesson 10 · Spring Data JPA Lesson 12 · REST → (coming next)