Introduction to Spring MVC
Get a high-level introduction to Spring MVC, the Model-View-Controller architecture, and the front-controller pattern that powers it.
Introduction
Everything you have learned so far, the container, dependency injection, configuration, and AOP, forms the foundation that Spring MVC builds on to handle web requests. Spring MVC is Spring's framework for building web applications and REST APIs, structured around the well-known Model-View-Controller pattern.
- What the Model-View-Controller pattern is
- What a front controller does and why Spring MVC uses one
- How to write a minimal Spring MVC controller
- The main building blocks Spring MVC is made of
What Is MVC?
MVC splits an application into three responsibilities. The Model holds application data. The View is responsible for rendering that data, typically as HTML or JSON. The Controller receives input, such as an HTTP request, decides what should happen, and selects the model and view used to produce a response.
Model
The data being worked with, such as an Order object or a list of products.
View
How the data is presented to the client, e.g. a Thymeleaf template or a JSON payload.
Controller
Receives the request, coordinates with services, and decides which model and view to use.
The Front-Controller Pattern
Rather than letting every URL map directly to its own separate servlet, Spring MVC funnels every incoming request through one central component: DispatcherServlet. This single entry point is responsible for figuring out which controller should handle a given request, invoking it, and rendering the result. This design is called the front-controller pattern, and it keeps request-handling logic (like logging, security, and error handling) centralized instead of duplicated across many endpoints.
You will look at DispatcherServlet's internal flow in detail in the next lesson. For now, just know that it is the single front door every request passes through.
A Minimal Spring MVC Controller
A controller is a class annotated with @Controller (or @RestController for APIs), with methods mapped to specific URL patterns using annotations like @GetMapping.
@RestControllerpublic class HomeController {
@GetMapping("/hello") public String hello() { return "Hello from Spring MVC!"; }}Click Run to see what this code prints.
The Building Blocks of Spring MVC
| Component | Role |
|---|---|
| DispatcherServlet | The front controller that every request passes through |
| HandlerMapping | Figures out which controller method should handle a given URL |
| Controller | Contains the handler methods that process requests |
| ViewResolver | Turns a logical view name into an actual view to render |
Common Mistakes
- Confusing @Controller (returns a view name) with @RestController (writes the response body directly), leading to unexpected 404s or raw text where JSON was expected.
- Assuming MVC is only for rendering HTML pages; it is equally the foundation for building JSON REST APIs.
- Putting business logic directly inside a controller method instead of delegating to a service.
- Not mapping a distinct HTTP method (GET, POST, etc.), which can make an endpoint respond to requests it was never meant to handle.
Best Practices
- Use @RestController for JSON APIs and reserve @Controller for endpoints that render server-side views.
- Keep controller methods short: parse input, delegate to a service, return a result.
- Group related endpoints under a shared @RequestMapping base path on the controller class.
- Map each endpoint to the specific HTTP method it is meant to handle (@GetMapping, @PostMapping, and so on).
Frequently Asked Questions
No. The same MVC building blocks power both traditional HTML-rendering applications and JSON REST APIs; @RestController is simply @Controller plus @ResponseBody on every method.
Not with Spring Boot, which embeds a servlet container like Tomcat directly inside the application, so you can run it as a standalone executable.
Spring MVC beans, like controllers, live in the same kind of ApplicationContext you have used throughout this course; it is a specialized web-aware child context layered on top of the core container concepts.
Key Takeaways
- Spring MVC structures web applications around the Model-View-Controller pattern.
- It uses the front-controller pattern: every request passes through a single DispatcherServlet.
- Controllers are classes annotated with @Controller or @RestController, with methods mapped to URLs.
- HandlerMapping and ViewResolver are core building blocks that route requests and resolve views.
- The same MVC foundation supports both HTML-rendering applications and JSON REST APIs.
Summary
Spring MVC organizes web applications around clear responsibilities and a single, central entry point. You have seen the concepts at a high level; next you will trace an actual request step by step through DispatcherServlet to see exactly how it finds and invokes the right controller.