Spring MVC Basics Interview Questions and Answers

Master Spring MVC fundamentals with interview questions covering DispatcherServlet, MVC architecture, controllers, request lifecycle, ViewResolver, Model, REST vs MVC, and production-ready web applications.


Introduction

Most enterprise Java web applications are built using the Model-View-Controller (MVC) architecture.

Examples include

  • Banking Portals
  • Insurance Applications
  • E-Commerce Websites
  • Hospital Management Systems
  • ERP Applications
  • Government Portals

Spring MVC is the web framework in the Spring ecosystem that simplifies building scalable, maintainable, and testable web applications.

At the heart of Spring MVC is the DispatcherServlet, which acts as the Front Controller, routing every incoming HTTP request to the appropriate component.


Spring MVC Architecture

flowchart LR

Client --> DispatcherServlet

DispatcherServlet --> Controller

Controller --> Service

Service --> Repository

Repository --> Database

Service --> Model

Model --> ViewResolver

ViewResolver --> View

View --> Client

Q1. What is Spring MVC?

Answer

Spring MVC is a web framework built on top of the Spring Framework that follows the Model-View-Controller design pattern.

Responsibilities

  • Handle HTTP requests
  • Route requests
  • Invoke business logic
  • Prepare response data
  • Render HTML or JSON

Spring MVC separates presentation, business logic, and data access, making applications easier to maintain.


Q2. What is the MVC Architecture?

MVC divides an application into three layers.

Component Responsibility
Model Holds application data
View Displays data to the user
Controller Handles incoming requests

MVC Pattern

flowchart LR

User --> Controller

Controller --> Service

Service --> Repository

Repository --> Database

Service --> Model

Model --> View

View --> User

Benefits

  • Loose coupling
  • Better testing
  • Easier maintenance
  • Code reuse

Q3. What is DispatcherServlet?

The DispatcherServlet is the Front Controller of Spring MVC.

Responsibilities

  • Receive every HTTP request
  • Find the correct controller
  • Invoke handler methods
  • Process exceptions
  • Resolve views
  • Return responses

Every web request passes through the DispatcherServlet.


Q4. How does DispatcherServlet work?

Request lifecycle

  1. Client sends request.
  2. DispatcherServlet receives it.
  3. HandlerMapping finds the controller.
  4. Controller executes business logic.
  5. Model is prepared.
  6. ViewResolver finds the view.
  7. Response is returned.

DispatcherServlet Flow

sequenceDiagram
Client->>DispatcherServlet: HTTP Request
DispatcherServlet->>HandlerMapping: Find Controller
HandlerMapping-->>DispatcherServlet: Controller
DispatcherServlet->>Controller: Invoke
Controller->>Service: Business Logic
Service-->>Controller: Result
Controller-->>DispatcherServlet: Model
DispatcherServlet->>ViewResolver: Resolve View
ViewResolver-->>DispatcherServlet: JSP/Thymeleaf
DispatcherServlet-->>Client: HTTP Response

Q5. What is a Controller?

A Controller processes incoming HTTP requests.

Example

@Controller
public class CustomerController {

}

For REST APIs

@RestController
public class CustomerApi {

}

Controllers should delegate business logic to service classes.


Q6. What is a Model?

The Model carries data from the controller to the view.

Example

model.addAttribute(
    "customer",
    customer
);

The View accesses model attributes to render the response.


Q7. What is a ViewResolver?

A ViewResolver determines which view should render the response.

Examples

  • Thymeleaf
  • JSP
  • FreeMarker
  • Mustache

View Resolution

flowchart LR

Controller --> ViewResolver

ViewResolver --> Thymeleaf

ViewResolver --> JSP

Thymeleaf --> Browser

In REST APIs, ViewResolvers are generally not used because JSON is returned directly.


Q8. What is the difference between @Controller and @RestController?

@Controller @RestController
Returns View Returns JSON/XML
Uses ViewResolver Uses HttpMessageConverter
Web Applications REST APIs

Example

@RestController

@RequestMapping("/customers")

@RestController is effectively @Controller + @ResponseBody.


Q9. What is HandlerMapping?

HandlerMapping maps incoming requests to controller methods.

Example

@GetMapping("/customers")

The DispatcherServlet asks HandlerMapping which controller should process the request.


Q10. Spring MVC Best Practices

Keep Controllers Thin

Controllers should only handle HTTP concerns.


Move Business Logic to Services

Maintain separation of concerns.


Validate Requests

Use Bean Validation annotations.


Implement Global Exception Handling

Use @ControllerAdvice.


Return Standard API Responses

Provide consistent success and error structures.


Banking Example

flowchart TD

Customer --> DispatcherServlet

DispatcherServlet --> PaymentController

PaymentController --> PaymentService

PaymentService --> PaymentRepository

PaymentRepository --> PostgreSQL

PaymentService --> JSONResponse

JSONResponse --> Customer

The controller handles the request while the service performs the business operation.


Common Interview Questions

  • What is Spring MVC?
  • What is the MVC architecture?
  • What is DispatcherServlet?
  • Explain the Spring MVC request lifecycle.
  • What is a Controller?
  • What is a Model?
  • What is a ViewResolver?
  • Difference between @Controller and @RestController?
  • What is HandlerMapping?
  • Spring MVC best practices?

Quick Revision

Topic Summary
Spring MVC Spring web framework
MVC Model-View-Controller architecture
DispatcherServlet Front Controller
Controller Handles HTTP requests
Model Transfers data to the view
ViewResolver Resolves the view
HandlerMapping Maps URL to controller
@Controller Returns views
@RestController Returns JSON/XML
Service Layer Contains business logic

Spring MVC Request Lifecycle

sequenceDiagram
Browser->>DispatcherServlet: HTTP Request
DispatcherServlet->>HandlerMapping: Find Handler
HandlerMapping-->>DispatcherServlet: Controller
DispatcherServlet->>Controller: Execute
Controller->>Service: Business Logic
Service->>Repository: Database
Repository-->>Service: Result
Service-->>Controller: Response
Controller-->>DispatcherServlet: Model / JSON
DispatcherServlet->>ViewResolver: Resolve View (MVC)
ViewResolver-->>DispatcherServlet: HTML
DispatcherServlet-->>Browser: Response

Production Example – Banking Account Summary

A banking application exposes an endpoint to retrieve customer account information.

Workflow

  1. Customer opens the mobile banking application.
  2. A GET request is sent to /accounts/{id}.
  3. The DispatcherServlet receives the request.
  4. HandlerMapping locates AccountController.
  5. AccountController delegates to AccountService.
  6. The service retrieves data from the database through AccountRepository.
  7. The controller returns the account information as JSON.
  8. The client displays the account balance and transaction summary.
@RestController
@RequestMapping("/accounts")
public class AccountController {

    @GetMapping("/{id}")
    public AccountDto getAccount(
            @PathVariable Long id) {

        return accountService.getAccount(id);
    }

}
flowchart LR

MobileApp --> DispatcherServlet

DispatcherServlet --> AccountController

AccountController --> AccountService

AccountService --> AccountRepository

AccountRepository --> PostgreSQL

AccountService --> AccountDto

AccountDto --> MobileApp

Benefits

  • Clear separation of concerns.
  • Easily testable controllers.
  • Reusable business logic.
  • REST-ready architecture.
  • Scalable enterprise design.

Spring MVC vs Spring Boot

Feature Spring MVC Spring Boot
Purpose Web Framework Application Framework
Focus HTTP Request Processing Auto Configuration & Application Setup
DispatcherServlet Core Component Auto-configured
Embedded Server Not Included Included (Tomcat, Jetty, Undertow)
Configuration Manual (Traditional) Mostly Automatic
REST Support Yes Yes (via Spring MVC)

Key Takeaways

  • Spring MVC is Spring's web framework based on the Model-View-Controller design pattern.
  • DispatcherServlet acts as the Front Controller and coordinates the complete request-processing lifecycle.
  • Controllers handle HTTP requests, while business logic belongs in the Service layer.
  • HandlerMapping identifies the appropriate controller method for each incoming request.
  • Model transfers data to the view, and ViewResolver determines which view should render the response.
  • @RestController is preferred for REST APIs because it automatically returns JSON or XML responses.
  • Keeping controllers thin and using global exception handling leads to cleaner, more maintainable applications.
  • Spring MVC provides the foundation for most enterprise Java web applications and REST services built with Spring Boot.