Spring MVC and REST
Cert Focus
You should understand the request lifecycle and how controllers map requests, read input, return responses, validate data, and handle errors.
Must Know
DispatcherServletis the front controller.@Controllerreturns views unless response body behavior is used.@RestControllercombines@Controllerand@ResponseBody.@RequestMappingand shortcut annotations map requests.@PathVariable,@RequestParam, and@RequestBodybind request data.@Validtriggers validation.@ExceptionHandlerand@ControllerAdvicecentralize error handling.
Direct Book Links
- Spring MVC Mental Model
- Request Mapping Deep Dive
- Request and Response Bodies
- Exception Handling in Spring MVC
- Spring MVC Validation
- Week 4 Review
Exam Trap
@RequestBody reads the HTTP body. @ResponseBody writes the return value to the HTTP response body.
Self-Check
- Can I explain
DispatcherServlet? - Can I choose between
@RequestParam,@PathVariable, and@RequestBody? - Can I explain how validation is triggered?
- Can I explain how global exception handling works?
Model Answers
DispatcherServlet is the front controller. It receives HTTP requests and coordinates handler mapping, binding, controller execution, and response rendering.
Use @RequestParam for query parameters or form values, @PathVariable for values inside the URL path, and @RequestBody for JSON or other request body content.
Validation is commonly triggered by adding @Valid or @Validated to a controller method parameter and having validation constraints on the target object.
Global exception handling is commonly implemented with @ControllerAdvice plus @ExceptionHandler, so multiple controllers can share consistent error responses.
Memory Sentence
Spring MVC routes the request, binds input, calls the controller, and writes the response.