org.springframework.web.servlet.handler

Class AbstractHandlerMethodMapping<T>

    • Constructor Detail

      • AbstractHandlerMethodMapping

        public AbstractHandlerMethodMapping()
    • Method Detail

      • setDetectHandlerMethodsInAncestorContexts

        public void setDetectHandlerMethodsInAncestorContexts(boolean detectHandlerMethodsInAncestorContexts)
        Whether to detect handler methods in beans in ancestor ApplicationContexts.

        Default is "false": Only beans in the current ApplicationContext are considered, i.e. only in the context that this HandlerMapping itself is defined in (typically the current DispatcherServlet's context).

        Switch this flag on to detect handler beans in ancestor contexts (typically the Spring root WebApplicationContext) as well.

        See Also:
        getCandidateBeanNames()
      • setHandlerMethodMappingNamingStrategy

        public void setHandlerMethodMappingNamingStrategy(HandlerMethodMappingNamingStrategy<T> namingStrategy)
        Configure the naming strategy to use for assigning a default name to every mapped handler method.

        The default naming strategy is based on the capital letters of the class name followed by "#" and then the method name, e.g. "TC#getFoo" for a class named TestController with method getFoo.

      • getHandlerMethods

        public java.util.Map<T,HandlerMethod> getHandlerMethods()
        Return a (read-only) map with all mappings and HandlerMethod's.
      • registerMapping

        public void registerMapping(T mapping,
                                    java.lang.Object handler,
                                    java.lang.reflect.Method method)
        Register the given mapping.

        This method may be invoked at runtime after initialization has completed.

        Parameters:
        mapping - the mapping for the handler method
        handler - the handler
        method - the method
      • unregisterMapping

        public void unregisterMapping(T mapping)
        Un-register the given mapping.

        This method may be invoked at runtime after initialization has completed.

        Parameters:
        mapping - the mapping to unregister
      • registerHandlerMethod

        protected void registerHandlerMethod(java.lang.Object handler,
                                             java.lang.reflect.Method method,
                                             T mapping)
        Register a handler method and its unique mapping. Invoked at startup for each detected handler method.
        Parameters:
        handler - the bean name of the handler or the handler instance
        method - the method to register
        mapping - the mapping conditions associated with the handler method
        Throws:
        java.lang.IllegalStateException - if another method was already registered under the same mapping
      • createHandlerMethod

        protected HandlerMethod createHandlerMethod(java.lang.Object handler,
                                                    java.lang.reflect.Method method)
        Create the HandlerMethod instance.
        Parameters:
        handler - either a bean name or an actual handler instance
        method - the target method
        Returns:
        the created HandlerMethod
      • initCorsConfiguration

        @Nullable
        protected CorsConfiguration initCorsConfiguration(java.lang.Object handler,
                                                                    java.lang.reflect.Method method,
                                                                    T mapping)
        Extract and return the CORS configuration for the mapping.
      • handlerMethodsInitialized

        protected void handlerMethodsInitialized(java.util.Map<T,HandlerMethod> handlerMethods)
        Invoked after all handler methods have been detected.
        Parameters:
        handlerMethods - a read-only map with handler methods and mappings.
      • getHandlerInternal

        protected HandlerMethod getHandlerInternal(HttpServletRequest request)
                                            throws java.lang.Exception
        Look up a handler method for the given request.
        Specified by:
        getHandlerInternal in class AbstractHandlerMapping
        Parameters:
        request - current HTTP request
        Returns:
        the corresponding handler instance, or null if none found
        Throws:
        java.lang.Exception - if there is an internal error
      • handleMatch

        protected void handleMatch(T mapping,
                                   java.lang.String lookupPath,
                                   HttpServletRequest request)
        Invoked when a matching mapping is found.
        Parameters:
        mapping - the matching mapping
        lookupPath - mapping lookup path within the current servlet mapping
        request - the current request
      • handleNoMatch

        @Nullable
        protected HandlerMethod handleNoMatch(java.util.Set<T> mappings,
                                                        java.lang.String lookupPath,
                                                        HttpServletRequest request)
                                                 throws java.lang.Exception
        Invoked when no matching mapping is not found.
        Parameters:
        mappings - all registered mappings
        lookupPath - mapping lookup path within the current servlet mapping
        request - the current request
        Throws:
        ServletException - in case of errors
        java.lang.Exception
      • isHandler

        protected abstract boolean isHandler(java.lang.Class<?> beanType)
        Whether the given type is a handler with handler methods.
        Parameters:
        beanType - the type of the bean being checked
        Returns:
        "true" if this a handler type, "false" otherwise.
      • getMappingForMethod

        @Nullable
        protected abstract T getMappingForMethod(java.lang.reflect.Method method,
                                                           java.lang.Class<?> handlerType)
        Provide the mapping for a handler method. A method for which no mapping can be provided is not a handler method.
        Parameters:
        method - the method to provide a mapping for
        handlerType - the handler type, possibly a sub-type of the method's declaring class
        Returns:
        the mapping, or null if the method is not mapped
      • getMappingPathPatterns

        protected abstract java.util.Set<java.lang.String> getMappingPathPatterns(T mapping)
        Extract and return the URL paths contained in a mapping.
      • getMatchingMapping

        @Nullable
        protected abstract T getMatchingMapping(T mapping,
                                                          HttpServletRequest request)
        Check if a mapping matches the current request and return a (potentially new) mapping with conditions relevant to the current request.
        Parameters:
        mapping - the mapping to get a match for
        request - the current HTTP servlet request
        Returns:
        the match, or null if the mapping doesn't match
      • getMappingComparator

        protected abstract java.util.Comparator<T> getMappingComparator(HttpServletRequest request)
        Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.
        Parameters:
        request - the current request
        Returns:
        the comparator (never null)