HandlerMapping, HandlerAdapter & Argument Resolution
Master HandlerMapping metadata construction, HandlerAdapter invocation, method argument resolution, and Jackson HttpMessageConverter JSON pipelines.
HandlerMapping, HandlerAdapter & Argument Resolution
To transform an incoming HTTP request into a Java method call, Spring MVC relies on three decoupled subsystems: HandlerMapping, HandlerAdapter, and HandlerMethodArgumentResolver.
1. HandlerMapping & Startup Mapping Construction
HandlerMapping answers the question: "Which controller method should handle this HTTP request?"
GET /students/10 ──► HandlerMapping ──► Returns StudentController.getStudent(Integer id)Startup-Time Mapping Optimization
Spring MVC does NOT scan controller annotations during runtime HTTP requests. Instead, during application startup:
APPLICATION STARTUP LIFECYCLE:
1. @ComponentScan scans packages for @RestController beans
2. Spring instantiates Controller beans
3. HandlerMapping inspects method annotations (@GetMapping, @PostMapping)
4. Constructs an in-memory Routing Table:
GET /students/{id} ──► StudentController.getStudent(Integer)
POST /students ──► StudentController.createStudent(Student)
PUT /students/{id} ──► StudentController.updateStudent(Integer, Student)At runtime, HandlerMapping performs an $O(1)$ indexed lookup against this pre-built mapping metadata.
2. HandlerAdapter & The Adapter Design Pattern
HandlerMapping identifies the target method, but does NOT execute it. HandlerAdapter executes the method:
DispatcherServlet ──► HandlerAdapter.handle(request, response, handler) ──► Invokes Controller MethodWhy an Adapter Layer Exists
Spring MVC supports multiple controller programming styles (annotated @RestController, functional RouterFunction, legacy Controller interfaces). The Adapter Pattern decouples DispatcherServlet from specific handler signatures.
3. Method Argument Resolution (HandlerMethodArgumentResolver)
HTTP request data arrives via multiple distinct channels. Argument resolvers extract and convert wire data into Java parameters:
@RestController
@RequestMapping("/students")
public class StudentController {
// 1. Path Variable: Extracts /students/10 ──► Integer id = 10
@GetMapping("/{id}")
public Student getStudent(@PathVariable Integer id) {
return studentService.getStudent(id);
}
// 2. Query Parameter: Extracts /students?name=Rahul ──► String name = "Rahul"
@GetMapping("/search")
public List<Student> searchStudents(@RequestParam String name) {
return studentService.searchByName(name);
}
// 3. Request Body: Deserializes JSON payload ──► Student Java Object
@PostMapping
public Student createStudent(@RequestBody Student student) {
return studentService.createStudent(student);
}
}HTTP Data Channels & Spring Bindings
| HTTP Source Channel | Spring Annotation | Underlying Extraction Process |
|---|---|---|
| URI Path Segment | @PathVariable | Extracts template match (/students/{id}) & converts String ──► Integer |
| URL Query String | @RequestParam | Reads Servlet request.getParameter("name") |
| HTTP Request Body | @RequestBody | Delegates to HttpMessageConverter + Jackson JSON deserializer |
4. HttpMessageConverter & Jackson JSON Pipeline
When processing @RequestBody inputs or @ResponseBody return values, HttpMessageConverter coordinates JSON translation:
REQUEST PAYLOAD (Deserialization):
JSON Stream ──► HttpMessageConverter (MappingJackson2HttpMessageConverter) ──► Jackson ──► Student Java Object
RESPONSE PAYLOAD (Serialization):
Student Java Object ──► HttpMessageConverter ──► Jackson ──► JSON Bytes (Content-Type: application/json)❓ Knowledge Check
What is the difference between HandlerMapping and HandlerAdapter?
Which component converts JSON HTTP request bodies into Java objects in Spring MVC?
Front Controller & DispatcherServlet
Master Spring MVC core architecture, the Airport Control Tower mental model, Front Controller pattern vs multi-servlet chaos, and DispatcherServlet orchestration.
Manual Spring MVC & Embedded Tomcat Bootstrap
Master manual Spring MVC bootstrap without Spring Boot, including embedded Tomcat setup, AnnotationConfigWebApplicationContext, and DispatcherServlet registration.