Spring Web Request Lifecycle

Spring Web Request Lifecycle — describes the sequence of steps an HTTP request goes through in a Spring (Boot) application from arrival to response.


🧩 Lifecycle Steps

  1. Client sends HTTP request
    Browser, mobile app, or client sends request to the server.
  2. Servlet Container (Tomcat/Jetty)
    The request hits the Servlet container, which delegates it to DispatcherServlet.
  3. DispatcherServlet
    • Central Spring MVC component.
    • Receives all incoming requests (front controller).
    • Dispatches to appropriate controllers via HandlerMapping.
  4. Handler Mapping
    Determines which controller method (@RequestMapping) will handle the request.
  5. Handler Adapter
    Invokes the controller method and prepares the return value.
  6. Controller (@Controller / @RestController)
    Processes business logic and returns ModelAndView or object.
  7. View Resolver (for @Controller)
    If using templates (Thymeleaf, JSP), resolves which view to render.
    If @RestController, object is converted to JSON via HttpMessageConverter.
  8. Interceptors & Filters
    • Filter → runs before DispatcherServlet.
    • HandlerInterceptor → preHandle, postHandle, afterCompletion around controller execution.
    • ExceptionHandler → handles errors.
  9. Response to Client
    Response is serialized (JSON/HTML) and sent back via the Servlet container.

🧠 Key Points

  • Filters → Servlet container level (pre/post request).
  • Interceptors → Spring MVC level (controller execution).
  • DispatcherServlet → heart of Spring Web.
  • HttpMessageConverters → transform objects to JSON/XML automatically.

Leave a Reply