Spring MVC Request Lifecycle Interview Questions and Answers

Master the Spring MVC Request Lifecycle with interview questions covering HTTP request flow, DispatcherServlet, HandlerMapping, HandlerAdapter, Controllers, Interceptors, ViewResolver, HttpMessageConverter, Filters, and production request processing.


Introduction

One of the most frequently asked Spring MVC interview questions is:

"Explain the complete Spring MVC Request Lifecycle."

Every HTTP request—whether it is for an HTML page or a REST API—passes through several Spring MVC components before a response is returned.

Understanding this lifecycle is essential for

  • Debugging production issues
  • Performance tuning
  • Security implementation
  • Logging and monitoring
  • Exception handling
  • Enterprise application development

The DispatcherServlet coordinates the entire lifecycle.


Complete Request Lifecycle

flowchart LR

Browser --> Filter

Filter --> DispatcherServlet

DispatcherServlet --> HandlerMapping

HandlerMapping --> HandlerAdapter

HandlerAdapter --> Controller

Controller --> Service

Service --> Repository

Repository --> Database

Controller --> ViewResolver

ViewResolver --> HTML

Controller --> HttpMessageConverter

HttpMessageConverter --> JSON

HTML --> Browser

JSON --> Browser

Q1. What is the Spring MVC Request Lifecycle?

Answer

The Spring MVC Request Lifecycle describes how an HTTP request travels from the client to the server and back.

High-level flow

  1. Client sends request.
  2. Servlet Filter executes.
  3. DispatcherServlet receives request.
  4. HandlerMapping locates controller.
  5. HandlerAdapter invokes controller.
  6. Business logic executes.
  7. Response is generated.
  8. View or JSON is returned.

Q2. What happens before DispatcherServlet?

Before the request reaches Spring MVC,

it passes through the Servlet Filter Chain.

Examples

  • Spring Security Filter
  • CORS Filter
  • Logging Filter
  • Compression Filter

Filter Chain

flowchart LR

Browser --> Filter1

Filter1 --> Filter2

Filter2 --> Filter3

Filter3 --> DispatcherServlet

Filters work at the Servlet container level.


Q3. What happens inside DispatcherServlet?

DispatcherServlet coordinates the request.

Responsibilities

  • Find controller
  • Execute interceptors
  • Invoke controller
  • Handle exceptions
  • Generate response

DispatcherServlet

flowchart TD

DispatcherServlet --> HandlerMapping

DispatcherServlet --> HandlerAdapter

DispatcherServlet --> Controller

DispatcherServlet --> ViewResolver

Q4. What is HandlerMapping's role?

HandlerMapping identifies the controller method.

Example

@GetMapping("/payments")

Incoming request

GET /payments

HandlerMapping returns

PaymentController

Q5. What is HandlerAdapter's role?

Controllers may have different signatures.

HandlerAdapter

  • Invokes controller
  • Resolves parameters
  • Handles return values

Example

public ResponseEntity<>

getPayment(...)

DispatcherServlet never directly invokes controller methods.


Q6. What happens inside the Controller?

Controller responsibilities

  • Validate request
  • Delegate business logic
  • Prepare response

Avoid

  • SQL
  • Business calculations
  • External API logic

Controller Flow

flowchart LR

Controller --> Service

Service --> Repository

Repository --> Database

Controllers should remain lightweight.


Q7. How is the response generated?

MVC Application

Controller

↓

ViewResolver

↓

HTML

REST API

Controller

↓

HttpMessageConverter

↓

JSON

Response Generation

flowchart TD

Controller --> ViewResolver

Controller --> HttpMessageConverter

ViewResolver --> HTML

HttpMessageConverter --> JSON

Q8. Where do Interceptors execute?

Interceptors run around controller execution.

Methods

  • preHandle()
  • postHandle()
  • afterCompletion()

Interceptor Lifecycle

sequenceDiagram
Request->>Interceptor: preHandle
Interceptor->>Controller: Execute
Controller-->>Interceptor: postHandle
Interceptor-->>Response: afterCompletion

Common uses

  • Authentication
  • Logging
  • Auditing
  • Metrics

Q9. How are Exceptions handled?

If an exception occurs,

DispatcherServlet delegates to

  • @ExceptionHandler
  • @ControllerAdvice
  • HandlerExceptionResolver

Exception Flow

flowchart LR

Controller --> Exception

Exception --> ExceptionResolver

ExceptionResolver --> ErrorResponse

Global exception handling is recommended.


Q10. Request Lifecycle Best Practices

Keep Controllers Thin

Delegate business logic.


Validate Early

Reject invalid requests immediately.


Use Global Exception Handling

Avoid duplicated code.


Log Request IDs

Improve tracing.


Monitor Request Time

Detect performance bottlenecks.


Banking Example

flowchart TD

MobileApp --> SecurityFilter

SecurityFilter --> DispatcherServlet

DispatcherServlet --> PaymentInterceptor

PaymentInterceptor --> PaymentController

PaymentController --> PaymentService

PaymentService --> PostgreSQL

PaymentService --> JSON

JSON --> MobileApp

Every request follows the same lifecycle regardless of business functionality.


Common Interview Questions

  • Explain the Spring MVC Request Lifecycle.
  • What happens before DispatcherServlet?
  • What is HandlerMapping?
  • What is HandlerAdapter?
  • What happens inside a Controller?
  • How are REST responses generated?
  • What is HttpMessageConverter?
  • Where do Interceptors execute?
  • How are exceptions handled?
  • Request lifecycle best practices?

Quick Revision

Component Responsibility
Filter Pre-process HTTP request
DispatcherServlet Front Controller
HandlerMapping Locate controller
HandlerAdapter Invoke controller
Controller Handle request
Service Business logic
Repository Database access
ViewResolver Generate HTML
HttpMessageConverter Generate JSON/XML
ExceptionResolver Handle errors

Complete Spring MVC Request Lifecycle

sequenceDiagram
Browser->>Servlet Filter: HTTP Request
Servlet Filter->>DispatcherServlet: Forward Request
DispatcherServlet->>HandlerMapping: Find Controller
HandlerMapping-->>DispatcherServlet: Handler
DispatcherServlet->>Interceptor: preHandle()
Interceptor-->>DispatcherServlet: Continue
DispatcherServlet->>HandlerAdapter: Invoke
HandlerAdapter->>Controller: Execute
Controller->>Service: Business Logic
Service->>Repository: Database Query
Repository-->>Service: Data
Service-->>Controller: Result
Controller-->>DispatcherServlet: Response Object
DispatcherServlet->>Interceptor: postHandle()
DispatcherServlet->>HttpMessageConverter: Serialize JSON
HttpMessageConverter-->>DispatcherServlet: JSON
DispatcherServlet->>Interceptor: afterCompletion()
DispatcherServlet-->>Browser: HTTP Response

Production Example – Banking Fund Transfer Request

A customer initiates a fund transfer from a mobile banking application.

Workflow

  1. Mobile app sends:
POST /payments
  1. Request enters the Servlet Filter Chain.

  2. Spring Security validates the JWT.

  3. DispatcherServlet receives the request.

  4. HandlerMapping locates PaymentController.

  5. HandlerAdapter invokes the controller.

  6. Bean Validation validates the request payload.

  7. PaymentService:

    • Verifies account balance.
    • Executes fraud checks.
    • Performs database transaction.
  8. Controller returns PaymentResponse.

  9. HttpMessageConverter converts the object into JSON.

  10. DispatcherServlet returns HTTP 200.

@PostMapping("/payments")
public PaymentResponse transfer(
        @Valid
        @RequestBody PaymentRequest request){

    return paymentService.transfer(request);

}
flowchart LR

MobileApp --> SecurityFilter

SecurityFilter --> DispatcherServlet

DispatcherServlet --> HandlerMapping

HandlerMapping --> PaymentController

PaymentController --> PaymentService

PaymentService --> FraudService

PaymentService --> PostgreSQL

PaymentService --> PaymentResponse

PaymentResponse --> HttpMessageConverter

HttpMessageConverter --> JSON

JSON --> MobileApp

Performance Considerations

Stage Recommendation
Filters Keep lightweight
Controllers No business logic
Services Handle business rules
Database Use connection pooling
JSON Serialization Return DTOs instead of entities
Logging Use correlation IDs
Monitoring Measure request latency
Exception Handling Centralize using @ControllerAdvice

Request Lifecycle: MVC vs REST API

Step Traditional MVC REST API
DispatcherServlet
HandlerMapping
HandlerAdapter
Controller
ViewResolver
HttpMessageConverter
HTML Response
JSON Response

Key Takeaways

  • Every Spring MVC request is coordinated by the DispatcherServlet, which acts as the Front Controller.
  • Requests first pass through the Servlet Filter Chain, where cross-cutting concerns such as security and logging are handled.
  • HandlerMapping identifies the correct controller, while HandlerAdapter invokes it and resolves method arguments.
  • Controllers should focus only on HTTP request handling and delegate business logic to service classes.
  • MVC applications use a ViewResolver to render HTML, while REST APIs rely on HttpMessageConverters to produce JSON or XML.
  • Interceptors execute before and after controller methods, making them ideal for auditing, logging, and performance monitoring.
  • Global exception handling using @ControllerAdvice ensures consistent API responses and cleaner controller code.
  • Understanding the complete request lifecycle is fundamental for designing, debugging, securing, and optimizing enterprise Spring MVC applications.