Open In App

HandlerAdapters in Spring MVC

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

HandlerAdapter is in charge of actually invoking the handler. Since the handler is of type Object, any Handler type may be handled by the dispatcher servlet with the use of the HandlerAdapter. particular handler interfaces can be handled by particular HandlerAdapters that Spring offers.

HandlerAdapters in Spring MVC

HandlerMapping links a method with a URL for the DispatcherServlet to decide which method should be invoked by a certain request. The DispatcherServlet then invokes the operation using a HandlerAdapter.It is utilized in combination with HandlerMapping, which associates a certain URL with a method. Here is an example of HandlerAdapters in Spring MVC.

Java




public interface HandlerAdapter {
  
  //Check if controller is supported
  boolean supports(Object handler);   
  
  ModelAndView handle(HttpServletRequest rqst, 
                   HttpServletResponse rsp,
                      Object handler) 
    throws Exception;
  //your implementation
}


Maven Dependency:

We need to add the below maven dependency to the XML file.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>

Types of HandlerAdapter in Spring MVC

1. SimpleControllerHandlerAdapter

This is the handler adapter that Spring MVC has registered by default. It is used to pass a request to a controller object and interacts with classes that implement the Controller interface. We don’t need to configure any HandlerAdapters if a web application simply utilizes controllers since the framework uses this class as the default adapter for handling requests.

<beans ...>
<bean name="/greeting.html"
class="org.geeksforgeeks.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>

2. SimpleServletHandlerAdapter

Any Servlet may be used in conjunction with DispatcherServlet to manage requests thanks to this handler adapter. It calls the service() function of the relevant Servlet class to send the request from DispatcherServlet to it.

<bean name="simpleServletHandlerAdapter" 
class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter" />

3. AnnotationMethodHandlerAdapter

Based on HTTP routes, HTTP methods, and request parameters, this adapter translates Handler methods. The @RequestParam annotation is used to bind the request parameters. Be aware that RequestMappingHandlerAdapter has replaced this adapter, which was deprecated in the spring.

<beans ...>
<context:component-scan base-package="org.geeksforgeeks.spring.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

4. RequestMappingHandlerAdapter

Type HandlerMethod is supported by this adapter for handling. RequestMappingHandlerMapping is the new class that was introduced in Spring 3.1 and is utilized with this adapter. You may use custom resolvers by,

<beans ...>
<context:component-scan base-package="org.geeksforgeeks.spring.controller" />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>

5. HttpRequestHandlerAdapter

Handles org.springframework.web.HttpRequestHandler interface. The HttpRequestHandler is used for handler that process HttpRequests. This interface is normally used to generate binary responses and does now not have a ModelAndView return type. The spring remoting training including HttpInvokerServiceExporter and HessianServiceExporter enforce this interface.

Java




public class MyHttpRequestHandler implements HttpRequestHandler {
    
  @Override
  public void handleRequest (HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException {
      PrintWriter writer = response.getWriter();
      writer.write("response from MyHttpRequestHandler, uri: "+request.getRequestURI());
  }
}


Run the application

In the event when spring-mvc-handlers is the context root and the application is deployed on localhost using port 8082:

http://localhost:8082/spring-mvc-handlers/

Conclusion

We hope you learned something new from this article on the Java HandlerAdapters in Spring MVC. A HandlerMapping associates a method with a URL, enabling the DispatcherServlet to determine which method ought to be triggered by a given request. After that, a HandlerAdapter is used by the DispatcherServlet to call the method.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads