Handler Adapters
Once the DispatcherServlet finds the appropriate HandlerMapping, the request is then forwarded to the appropriate Controller class as defined in the Spring configuration file. As explained earlier, this is achieved because Spring’s default HandlerAdapter namely org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter kicks in and forwards the request from the DispatcherServlet to appropriate Controller class. The working of HandlerAdapter can be pictorially represented as shown in Figure 14-11 below.
Figure 14-11. Working of HandlerAdapter in overall request flowin Spring MVC
If HandlerMapping finds the Controller but not the method to be invoked, HandlerAdapter allows selecting an appropriate method in the controller as well. According to application demand if it requires special HandlerAdapter, you can implement the HandlerAdapter interface whose definition is shown in Listing 14-15 below.
Listing 14-15. HandlerAdapter interface definition
1 2 3 4 5 6 7 8 9 |
package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface HandlerAdapter { boolean supports(Object handler); ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Ob ject handler) throws Exception; long getLastModified(HttpServletRequest request, Object handler); } |
Spring has several built-in HandlerAdapter’s, some of which are as summarized as shown below:
- springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdpater – HandlerAdapter which maps the handler methods in the controller based on HTTP paths, HTTP methods and request parameters expressed through the RequestMapping annotation discussed in earlier sections of this Chapter.
- springframework.web.servlet.handler.SimpleServletHandlerAdapter – HandlerAdapter which calls the servlet’s “service” method for handling the passed-on request. This adapter is not activated by default and need to be specified as a bean in the Spring configuration file. After this it will automatically be applied to mapped handler beans that implement the Servlet interface.
Exception Resolvers
Execution of controller code in your application can throws various exceptions at various stages. HandlerExceptionResolver is SpringMVC’s strategy for handling controller exceptions. HandlerExceptionResolver Interface is to be implemented by objects than can resolve exceptions thrown during handler mapping or execution to error views. It prepares appropriate model and selects and error view and renders it to the user. Listing 14-16 below shows the HandlerExceptionResolver interface.
Listing 14-16. HandlerExceptionResolver interface
1 2 3 4 5 6 7 |
package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public interface HandlerExceptionResolver { ModelAndView resolveException(HttpServletRequest request, HttpServletResponse re sponse, Object handler, Exception ex); } |
Spring MVC does provide built-in HandlerExceptionResolver imlemnattions which can be summarized as below:
- springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver – resolves standard Spring exceptions and translates into corresponding HTTP status codes. This exception resolver is enabled by default in the DispatcherServlet.
- springframework.web.servlet.handler.SimpleMappingExceptionResolver – resolves exceptions and maps exception class to view names. The views will have a model attribute “exception” filled in accordingly. Listing 14-17 below shows configuring SimpleMappingExceptionResolver.
Listing 14-17. Configuring SimpleMappingExceptionResolver in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<beans …> <bean class="org.springframework.web.servlet.handler. SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <map> <entry key="DataAccessException" value="dbError"/> <entry key="InvalidBusinessException" value="error"/> </map> </property> <property name="defaultStatusCode" value="404"/> <property name="defaultErrorView" value="error"/> </bean> </beans> |
Page Visitors: 7592


Tomcy John


Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023