I am beginner in Spring MVC. I didn't understand handler adapters clearly. What is a handler adapter and when do I use adapters?
4 Answers
A HandlerMapping
maps a method to a URL, so the DispatcherServlet
knows which method should be invoked by a specific request. Then the DispatcherServlet
use a HandlerAdapter
to invoke the method.
Why DispatcherServlet does not invoke a method directly?
Because there are many ways to invoke a method, like annotation, xml etc. HandlerAdapter
de-couples the DispatcherServlet
and the invoked actions.

- 72,055
- 26
- 237
- 180

- 313
- 3
- 6
This section of the Spring docs discusses the default adapters and how they relate to annotation configuration.
Briefly, handler adapters decide which controller (and method) to call for a request.

- 386
- 5
- 20

- 158,873
- 26
- 254
- 302
-
The link is broken. – Vishnu Dec 13 '17 at 19:11
-
@Vishnu Things change in six years I guess. – Dave Newton Dec 13 '17 at 19:35
You can find Adapter in HandlerAdapter and that part of name comes from Adapter pattern. Adapter is like a bridge between two objects and HandlerAdapter is a bridge between handler object and dispatcher servlet.
As you can see from the HandlerAdapter source code below taken from Spring documentation, there is one method, handle method with ModelAndView return type. Every HandlerAdapter will implement this method to delegate the HttpServletRequest and HttpServletResponse to handler object so then handler object will execute application logic using these HttpServletRequest/Response.
public interface HandlerAdapter {
//Check if controller is supported
boolean supports(Object handler);
//handle request
ModelAndView handle(HttpServletRequest rqst,
HttpServletResponse rsp,
Object handler) throws Exception;
This application logic execution produces model and view. The view can be in form of view name String or View object. The model holds data that will be used to render the view. HandlerAdapter will wrap the model and view in ModelAndView object. It is dispatcher servlet job to process ModelAndView object.
Dispatcher servlet does not know about handler object and relieved from directly handle application logic. Handler object also relieved from converting model and view into ModelAndView object because HandlerAdapter will do that converting job.

- 321
- 2
- 9
- 19
A HandlerMapping simply maps a method to a URL. Most beginners don't use this object directly, but rather use RequestMapping's instead. The return type of the mapped method (generally) determines what view SpringMVC will use to render a response.
For example, the following RequestMapping will generate a HandlerMapping for GET requests to "/" or "/home" to invoke this method:
@RequestMapping(value={"/", "/home"}, method=RequestMethod.GET)
public String getHome() {
return "homepage";
}
The method returns a string name of a view, which would typically be resolved to "/WEB-INF/views/homepage.jsp" (but that depends on your ViewResolver of course)
Just an fyi for starting out: you can add different objects that you might need as parameters to your method (like Locale, HttpServletRequest, etc). See the RequestMapping javadoc for more info.

- 1,941
- 3
- 26
- 34