Cheatsheet
Spring MVC
Key idea
Spring MVC implements the Front Controller pattern: a single servlet, the `DispatcherServlet`, receives every request, asks the `HandlerMapping`s which controller method handles it, binds the parameters, invokes it and turns the result into a view (with a `ViewResolver`) or the response body (with an `HttpMessageConverter`).
Diagram (UML)
sequenceDiagram
actor B as Browser
participant T as Embedded Tomcat
participant D as DispatcherServlet
participant M as HandlerMapping
participant A as HandlerAdapter
participant C as ScheduleController
participant V as ViewResolver
B->>T: GET /schedule?day=mon
T->>D: service(request)
D->>M: getHandler(request)
M-->>D: ScheduleController.day(String, Model)
D->>A: handle(request, handler)
A->>C: day("mon", model) with converted params
C-->>A: "schedule" (logical view name)
A-->>D: ModelAndView
D->>V: resolveViewName("schedule")
V-->>D: templates/schedule.html
D-->>B: rendered HTML
Note over D,C: with @ResponseBody / @RestController an HttpMessageConverter writes the body instead of a viewKey pieces
DispatcherServlet: the front controller; Spring Boot registers it mapped to/HandlerMapping(URL → method),HandlerAdapter(invokes the method),ViewResolver(name → view)@Controller(returns view names) and@RestController(=@Controller+@ResponseBody)@RequestMappingon class and method; shortcuts@GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PatchMapping- Parameters:
@RequestParam,@PathVariable,@RequestHeader,@CookieValue,@RequestBody,Model ResponseStatusException,redirect:andforward:as return values
ScheduleController.java
@Controller // a web controller whose methods return VIEW names by default
@RequestMapping("/schedule") // prefix for every method below
public class ScheduleController {
private final ScheduleService schedule;
public ScheduleController(ScheduleService schedule) {
this.schedule = schedule;
}
@GetMapping // GET /schedule
public String day(@RequestParam(defaultValue = "mon") String day, Model model) {
model.addAttribute("day", day); // data for the view
model.addAttribute("classes", schedule.on(day));
return "schedule"; // logical view name → templates/schedule.html
}
// …
}Remember for the exam
- Flow: request →
DispatcherServlet→HandlerMapping→HandlerAdapter→ controller →ViewResolver/HttpMessageConverter→ response - A
Stringreturned by a@Controlleris a logical view name; with@ResponseBodyit's the body @RequestParamis required by default;required = falseordefaultValuemake it optional (defaultValueimpliesrequired = false)- Spring converts text to types (
"2"→long); if it can't, it answers 400 @RequestMappingon the class adds a prefix to every method- Spring Boot starts an embedded Tomcat, registers the
DispatcherServletand configures Jackson, static resources and view resolvers
Exam traps
- A missing required
@RequestParam→ 400 (MissingServletRequestParameterException), not 500 - A type that can't be converted (
/classes/twofor along) → 400 (MethodArgumentTypeMismatchException) - In a
@Controller, forgetting@ResponseBodymakes Spring look for a view with that name - An unnamed
@PathVariableneeds the parameter to be named like the URL variable (and compiling with-parameters)