Skip to main content

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

  • DispatcherServlet is the front controller.
  • @Controller returns views unless response body behavior is used.
  • @RestController combines @Controller and @ResponseBody.
  • @RequestMapping and shortcut annotations map requests.
  • @PathVariable, @RequestParam, and @RequestBody bind request data.
  • @Valid triggers validation.
  • @ExceptionHandler and @ControllerAdvice centralize error handling.

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.