Spring MVC interface HandlerMapping is used to abstract modeling concepts: mapping between request and handler.
The interface can be implemented by developers, but this is not necessary. The framework has its own implementation, such as BeanNameUrlHandlerMapping and RequestMappingHandlerMapping. If the HandlerMapping bean component is not registered in the container, BeanNameUrlHandlerMapping will be used by default.
HandlerMapping only defines a method HandlerExecutionChain getHandler(HttpServletRequest request), which is used to get the handle according to the request. Note that the handler obtained always exists in the form of a HandlerExecutionChain.
A HandlerExecutionChain represents a set of HandlerInterceptor objects (0 or more) and a real handler object. When the dispatcher servlet uses the HandlerExecutionChain to process the request:
- Always call the preHandle methods of all handlerinterceptors in the specified order, and finally call the handler when all prehandles return true.
- After the handler is executed, the method postHandle of each HandlerInterceptor is called in the reverse order of calling preHandle.
- Then, after the handler's execution result is processed, the method afterCompletion of each HandlerInterceptor is called according to the same property of executing preHandler.
package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import org.springframework.lang.Nullable; public interface HandlerMapping { /** * Name of the HttpServletRequest attribute that contains the mapped * handler for the best matching pattern. * @since 4.3.21 */ String BEST_MATCHING_HANDLER_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingHandler"; /** * Name of the HttpServletRequest attribute that contains the path * within the handler mapping, in case of a pattern match, or the full * relevant URI (typically within the DispatcherServlet's mapping) else. * Note: This attribute is not required to be supported by all * HandlerMapping implementations. URL-based HandlerMappings will * typically support it, but handlers should not necessarily expect * this request attribute to be present in all scenarios. */ String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping"; /** * Name of the HttpServletRequest attribute that contains the * best matching pattern within the handler mapping. * Note: This attribute is not required to be supported by all * HandlerMapping implementations. URL-based HandlerMappings will * typically support it, but handlers should not necessarily expect * this request attribute to be present in all scenarios. */ String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern"; /** * Name of the boolean HttpServletRequest attribute that indicates * whether type-level mappings should be inspected. * Note: This attribute is not required to be supported by all * HandlerMapping implementations. */ String INTROSPECT_TYPE_LEVEL_MAPPING = HandlerMapping.class.getName() + ".introspectTypeLevelMapping"; /** * Name of the HttpServletRequest attribute that contains the URI * templates map, mapping variable names to values. * Note: This attribute is not required to be supported by all * HandlerMapping implementations. URL-based HandlerMappings will * typically support it, but handlers should not necessarily expect * this request attribute to be present in all scenarios. */ String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables"; /** * Name of the HttpServletRequest attribute that contains a map with * URI variable names and a corresponding MultiValueMap of URI matrix * variables for each. * Note: This attribute is not required to be supported by all * HandlerMapping implementations and may also not be present depending on * whether the HandlerMapping is configured to keep matrix variable content */ String MATRIX_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".matrixVariables"; /** * Name of the HttpServletRequest attribute that contains the set of * producible MediaTypes applicable to the mapped handler. * Note: This attribute is not required to be supported by all * HandlerMapping implementations. Handlers should not necessarily expect * this request attribute to be present in all scenarios. */ String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes"; /** * Return a handler and any interceptors for this request. The choice may be made * on request URL, session state, or any factor the implementing class chooses. * The returned HandlerExecutionChain contains a handler Object, rather than * even a tag interface, so that handlers are not constrained in any way. * For example, a HandlerAdapter could be written to allow another framework's * handler objects to be used. * Returns null if no match was found. This is not an error. * The DispatcherServlet will query all registered HandlerMapping beans to find * a match, and only decide there is an error if none can find a handler. * @param request current HTTP request * @return a HandlerExecutionChain instance containing handler object and * any interceptors, or null if no mapping found * @throws Exception if there is an internal error */ @Nullable HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception; }
This interface mainly defines a method HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception. In addition, some constants are defined, which are used as attribute names when recording some attributes to HttpServletRequest during request / processor mapping.