Class MappedInterceptor
- All Implemented Interfaces:
HandlerInterceptor
HandlerInterceptor and uses URL patterns to determine whether
it applies to a given request.
Pattern matching can be done with a PathMatcher or with a parsed
PathPattern. The syntax is largely the same with the latter being more
tailored for web usage and more efficient. The choice is driven by the
presence of a resolved
String lookupPath or a parsed RequestPath which in turn depends on the HandlerMapping
that matched the current request.
MappedInterceptor is supported by subclasses of
AbstractHandlerMethodMapping which detect beans of type MappedInterceptor
and also check if interceptors directly registered with it are of this type.
- Since:
- 3.0
- Author:
- Keith Donald, Rossen Stoyanchev, Brian Clozel
-
Constructor Summary
ConstructorsConstructorDescriptionMappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods, HandlerInterceptor interceptor, @Nullable PathPatternParser parser) Create an instance with the given include and exclude patterns and HTTP methods.MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, WebRequestInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with aWebRequestInterceptoras the target.MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HandlerInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)without a provided parser.MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HandlerInterceptor interceptor, @Nullable PathPatternParser parser) Deprecated, for removal: This API element is subject to removal in a future version.in favor of the constructor variant with HTTP methodsMappedInterceptor(String @Nullable [] includePatterns, WebRequestInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with aWebRequestInterceptoras the target.MappedInterceptor(String @Nullable [] includePatterns, HandlerInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with include patterns only. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterCompletion(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, @Nullable Exception ex) Callback after completion of request processing, that is, after rendering the view.Get the exclude path patterns this interceptor is mapped to.Get the include path patterns this interceptor is mapped to.The targetHandlerInterceptorto invoke in case of a match.Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.booleanmatches(jakarta.servlet.http.HttpServletRequest request) Check whether this interceptor is mapped to the request.voidpostHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) Interception point after successful execution of a handler.booleanpreHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler) Interception point before the execution of a handler.voidsetPathMatcher(PathMatcher pathMatcher) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.
-
Constructor Details
-
MappedInterceptor
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods, HandlerInterceptor interceptor, @Nullable PathPatternParser parser) Create an instance with the given include and exclude patterns and HTTP methods.- Parameters:
includePatterns- patterns to match, or null to match all pathsexcludePatterns- patterns for which requests must not matchincludeHttpMethods- the HTTP methods to match, or null for all methodsexcludeHttpMethods- the ßHTTP methods to which requests must not matchinterceptor- the target interceptorparser- a parser to use to pre-parse patterns intoPathPattern; when not provided,PathPatternParser.defaultInstanceis used.- Since:
- 7.0
-
MappedInterceptor
@Deprecated(since="7.0", forRemoval=true) public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HandlerInterceptor interceptor, @Nullable PathPatternParser parser) Deprecated, for removal: This API element is subject to removal in a future version.in favor of the constructor variant with HTTP methodsVariation ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)without HTTP methods.- Since:
- 5.3
-
MappedInterceptor
Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with include patterns only. -
MappedInterceptor
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HandlerInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)without a provided parser. -
MappedInterceptor
Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with aWebRequestInterceptoras the target. -
MappedInterceptor
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, WebRequestInterceptor interceptor) Variant ofMappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)with aWebRequestInterceptoras the target.
-
-
Method Details
-
getIncludePathPatterns
-
getExcludePathPatterns
-
getInterceptor
The targetHandlerInterceptorto invoke in case of a match. -
setPathMatcher
Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.Configure the PathMatcher to use to match URL paths with against include and exclude patterns.This is an advanced property that should be used only when a customized
AntPathMatcheror a custom PathMatcher is required.By default this is
AntPathMatcher.Note: Setting
PathMatcherenforces use of String pattern matching even when a parsedRequestPathis available. -
getPathMatcher
Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.Get the configured PathMatcher. -
matches
public boolean matches(jakarta.servlet.http.HttpServletRequest request) Check whether this interceptor is mapped to the request.The request mapping path is expected to have been resolved externally. See also class-level Javadoc.
- Parameters:
request- the request to match to- Returns:
trueif the interceptor should be applied to the request
-
preHandle
public boolean preHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler) throws Exception Description copied from interface:HandlerInterceptorInterception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.
Note: special considerations apply for asynchronous request processing. For more details see
AsyncHandlerInterceptor.The default implementation returns
true.- Specified by:
preHandlein interfaceHandlerInterceptor- Parameters:
request- current HTTP requestresponse- current HTTP responsehandler- chosen handler to execute, for type and/or instance evaluation- Returns:
trueif the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.- Throws:
Exception- in case of errors
-
postHandle
public void postHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception Description copied from interface:HandlerInterceptorInterception point after successful execution of a handler. Called after HandlerAdapter actually invoked the handler, but before the DispatcherServlet renders the view. Can expose additional model objects to the view via the given ModelAndView.DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can post-process an execution, getting applied in inverse order of the execution chain.
Note: special considerations apply for asynchronous request processing. For more details see
AsyncHandlerInterceptor.The default implementation is empty.
- Specified by:
postHandlein interfaceHandlerInterceptor- Parameters:
request- current HTTP requestresponse- current HTTP responsehandler- the handler (orHandlerMethod) that started asynchronous execution, for type and/or instance examinationmodelAndView- theModelAndViewthat the handler returned (can also benull)- Throws:
Exception- in case of errors
-
afterCompletion
public void afterCompletion(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception Description copied from interface:HandlerInterceptorCallback after completion of request processing, that is, after rendering the view. Will be called on any outcome of handler execution, thus allows for proper resource cleanup.Note: Will only be called if this interceptor's
preHandlemethod has successfully completed and returnedtrue!As with the
postHandlemethod, the method will be invoked on each interceptor in the chain in reverse order, so the first interceptor will be the last to be invoked.Note: special considerations apply for asynchronous request processing. For more details see
AsyncHandlerInterceptor.The default implementation is empty.
- Specified by:
afterCompletionin interfaceHandlerInterceptor- Parameters:
request- current HTTP requestresponse- current HTTP responsehandler- the handler (orHandlerMethod) that started asynchronous execution, for type and/or instance examinationex- any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver- Throws:
Exception- in case of errors
-