I have been reading Spring MVC HandlerMapping
and HandlerAdapter
but I am getting confused between this two concepts.I can understand that HandlerMapping
is used to map incoming HTTP request to controller but what is the use of HandlerAdapter?
why do we use it?
what is the exact difference between these two with example?
Please help me on this.Thanks !!
Since introduction of RequestMappingHandlerMapping and RequestMappingHandlerAdapter in Spring 3.1 the distinction is even simpler: RequestMappingHandlerMapping finds the appropriate handler method for the given request. RequestMappingHandlerAdapter executes this method, providing it with all the arguments.
The HandlerMapping is used to Maps a request to Handlers i.e. Controllers. For example: DefaultAnnotationHandlerMapping, SimpleUrlHandlerMapping, BeanNameUrlHandlerMapping. DefaultAnnotationHandlerMapping.
<mvc:annotation-driven />
declares explicit support for annotation-driven MVC controllers. The tag configures two beans (Mapping and Adapter) DefaultAnnotationHandlerMapping
and AnnotationMethodHandlerAdapter
so that you do not need to declare them in context config file.
The HandlerAdapter is basically an interface which facilitates the handling of HTTP requests in a very flexible manner in Spring MVC. The DispatcherServlet doesn’t invoke the method directly – it basically serves as a bridge between itself and the handler objects, leading to a loosely coupling design.
public interface HandlerAdapter {
//check if a particular handler instance is supported or not.
boolean supports(Object handler);
//used to handle a particular HTTP request and returns ModelAndView object to DispatcherServlet
ModelAndView handle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
}
Types of HandlerAdapter :
AnnotationMethodHandlerAdapter : which executes methods annotated with @RequestMapping
. AnnotationMethodHandlerAdapter was deprecated and replace by RequestMappingHandlerAdapter from Spring 3.1+.
SimpleControllerHandlerAdapter: This is the default handler adapter registered by Spring MVC. It deals with classes implementing Controller interface and is used to forward a request to a controller object.
If a web application uses only controllers then we don’t need to configure any HandlerAdapter as the framework uses this class as the default adapter for handling a request.
Let’s define a simple controller class, using the older style of controller (implementing the Controller interface):
public class SimpleController implements Controller {
@Override
public ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("Greeting");
model.addObject("message", "Dinesh Madhwal");
return model;
}
}
Similar XML configuration:
<beans ...>
<bean name="/greeting.html"
class="com.baeldung.spring.controller.SimpleControllerHandlerAdapterExample"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
for more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With