How does a request flow through Spring MVC?
Options
- The browser calls the controller method straight away
DispatcherServletmaps the request to a controller (Correct option)- The database receives the request first and forwards it
- The request is stored in a queue and processed later
Answer
The embedded server (Tomcat) receives it, the DispatcherServlet (front controller) maps it to a controller method, which calls services; the result is converted to JSON and returned
Why
- The embedded servlet container (Tomcat) receives the HTTP request.
- Filters run (security, logging).
- The
DispatcherServletis the single entry point: it finds which controller method handles that URL and HTTP method. - Arguments are resolved (
@PathVariable,@RequestBodyvia Jackson) and the controller runs, calling services and repositories. - The return value is converted (usually to JSON) and written to the response; exceptions go to
@ControllerAdvice.
Remember
Tomcat, filters, DispatcherServlet, controller, service, repository.