WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Intermediate · question 34 of 100

Explain the use of HandlerMapping and HandlerAdapter in Spring MVC.?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

In Spring MVC, ‘HandlerMapping‘ and ‘HandlerAdapter‘ are essential components that work together to handle incoming requests and invoke the appropriate controller method for processing.

‘HandlerMapping‘ is responsible for identifying the appropriate controller method to handle an incoming request. There are several implementations of ‘HandlerMapping‘, each with a different strategy for mapping requests to controller methods. Some common implementations include:

- ‘RequestMappingHandlerMapping‘: This implementation maps requests based on ‘@RequestMapping‘ annotations on controller methods. - ‘BeanNameUrlHandlerMapping‘: This implementation maps requests based on the URL path using the names of the controller beans.

‘HandlerAdapter‘ is responsible for invoking the appropriate controller method identified by the ‘HandlerMapping‘ and preparing the output for the view layer. There are several implementations of ‘HandlerAdapter‘, each designed to support different types of controller methods. Some common implementations include:

- ‘RequestMappingHandlerAdapter‘: This implementation supports methods annotated with ‘@RequestMapping‘. It handles resolving method arguments and preparing the response. - ‘SimpleControllerHandlerAdapter‘: This implementation supports controllers implementing the ‘Controller‘ interface. It simply invokes the ‘handleRequest‘ method on the controller.

Together, ‘HandlerMapping‘ and ‘HandlerAdapter‘ make up the controller layer of a Spring MVC application. They work together to process incoming requests, invoke the appropriate controller method, and prepare the response for the view layer.

For example, let’s consider we have a Spring MVC application that has a controller named ‘UserController‘ with a method ‘getUserDetails()‘ that receives a request from URL "/users/1". The ‘BeanNameUrlHandlerMapping‘ implementation of ‘HandlerMapping‘ can find the appropriate controller bean, and the ‘RequestMappingHandlerAdapter‘ implementation of ‘HandlerAdapter‘ can invoke the ‘getUserDetails()‘ method and prepare the response.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics