Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.handler

public final Class MappedInterceptor

extends Object
implements HandlerInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.HandlerInterceptor
Imports
java.util.Arrays, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.http.server.PathContainer, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .ObjectUtils, .PathMatcher, org.springframework.web.context.request.WebRequestInterceptor, org.springframework.web.servlet.HandlerInterceptor, .HandlerMapping, .ModelAndView, org.springframework.web.util.ServletRequestPathUtils, .UrlPathHelper, org.springframework.web.util.pattern.PathPattern, .PathPatternParser, .PatternParseException

Wraps a 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.

Authors
Keith Donald, Rossen Stoyanchev, Brian Clozel
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MappedInterceptor.PatternAdapter

Contains both the parsed PathPattern and the raw String pattern, and uses the former when the cached path is PathContainer or the latter otherwise.

Field Summary

Modifier and TypeField and Description
private static final PathMatcher
private final MappedInterceptor.PatternAdapter[]
private final MappedInterceptor.PatternAdapter[]
private final HandlerInterceptor
private PathMatcher

Constructor Summary

AccessConstructor and Description
public
MappedInterceptor(String[]
patterns to which requests must match, or null to match all paths
includePatterns
,
String[]
patterns to which requests must not match
excludePatterns
,
HandlerInterceptor
the target interceptor
interceptor
,
PathPatternParser
a parser to use to pre-parse patterns into PathPattern; when not provided, PathPatternParser#defaultInstance is used.
parser
)

Create an instance with the given include and exclude patterns along with the target interceptor for the mappings.

public
public
MappedInterceptor(String[] includePatterns, String[] excludePatterns, HandlerInterceptor interceptor)

Variant of MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser) without a provided parser.

public
public

Method Summary

Modifier and TypeMethod and Description
public void
afterCompletion(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination
handler
,
Exception
any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver
ex
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

Callback after completion of request processing, that is, after rendering the view.

public String[]
getExcludePathPatterns()

Get the exclude path patterns this interceptor is mapped to.

public String[]
getIncludePathPatterns()

Get the include path patterns this interceptor is mapped to.

public HandlerInterceptor
getInterceptor()

The target HandlerInterceptor to invoke in case of a match.

public PathMatcher
getPathMatcher()

Get the configured PathMatcher.

public String[]
getPathPatterns()
Deprecated for removal since 6.1. since 6.1 in favor of getIncludePathPatterns()

Get the include path patterns this interceptor is mapped to.

public boolean

Returns:

true if the interceptor should be applied to the request
matches
(HttpServletRequest
the request to match to
request
)

Check whether this interceptor is mapped to the request.

public boolean

Returns:

true if the interceptor applies to the given request path
matches
(String
the current request path
lookupPath
,
PathMatcher
a path matcher for path pattern matching
pathMatcher
)
Deprecated since 5.3. as of 5.3 in favor of matches(HttpServletRequest)

Determine if there is a match for the given lookup path.

public void
postHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination
handler
,
ModelAndView
the ModelAndView that the handler returned (can also be null)
modelAndView
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.postHandle.

Interception point after successful execution of a handler.

public boolean
preHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
chosen handler to execute, for type and/or instance evaluation
handler
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler.

public void
setPathMatcher(PathMatcher pathMatcher)

Configure the PathMatcher to use to match URL paths with against include and exclude patterns.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultPathMatcherback to summary
private static final PathMatcher defaultPathMatcher
excludePatternsback to summary
private final MappedInterceptor.PatternAdapter[] excludePatterns
Annotations
@Nullable
includePatternsback to summary
private final MappedInterceptor.PatternAdapter[] includePatterns
Annotations
@Nullable
interceptorback to summary
private final HandlerInterceptor interceptor
pathMatcherback to summary
private PathMatcher pathMatcher

Constructor Detail

MappedInterceptorback to summary
public MappedInterceptor(String[] includePatterns, String[] excludePatterns, HandlerInterceptor interceptor, PathPatternParser parser)

Create an instance with the given include and exclude patterns along with the target interceptor for the mappings.

Parameters
includePatterns:String[]

patterns to which requests must match, or null to match all paths

excludePatterns:String[]

patterns to which requests must not match

interceptor:HandlerInterceptor

the target interceptor

parser:PathPatternParser

a parser to use to pre-parse patterns into PathPattern; when not provided, PathPatternParser#defaultInstance is used.

Since
5.3
MappedInterceptorback to summary
public MappedInterceptor(String[] includePatterns, HandlerInterceptor interceptor)

Variant of MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser) with include patterns only.

MappedInterceptorback to summary
public MappedInterceptor(String[] includePatterns, String[] excludePatterns, HandlerInterceptor interceptor)

Variant of MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser) without a provided parser.

MappedInterceptorback to summary
public MappedInterceptor(String[] includePatterns, WebRequestInterceptor interceptor)

Variant of MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser) with a WebRequestInterceptor as the target.

MappedInterceptorback to summary
public MappedInterceptor(String[] includePatterns, String[] excludePatterns, WebRequestInterceptor interceptor)

Variant of MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser) with a WebRequestInterceptor as the target.

Method Detail

afterCompletionback to summary
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception

Overrides default org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

Callback 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 preHandle method has successfully completed and returned true!

As with the postHandle method, 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 org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation is empty.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination

ex:Exception

any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver

Annotations
@Override
Exceptions
Exception:
in case of errors
getExcludePathPatternsback to summary
public String[] getExcludePathPatterns()

Get the exclude path patterns this interceptor is mapped to.

Annotations
@Nullable
Since
6.1
See Also
getIncludePathPatterns()
getIncludePathPatternsback to summary
public String[] getIncludePathPatterns()

Get the include path patterns this interceptor is mapped to.

Annotations
@Nullable
Since
6.1
See Also
getExcludePathPatterns()
getInterceptorback to summary
public HandlerInterceptor getInterceptor()

The target HandlerInterceptor to invoke in case of a match.

getPathMatcherback to summary
public PathMatcher getPathMatcher()

Get the configured PathMatcher.

getPathPatternsback to summary
public String[] getPathPatterns()

Deprecated

for removal since 6.1.

since 6.1 in favor of getIncludePathPatterns()

Get the include path patterns this interceptor is mapped to.

Annotations
@Nullable
@Deprecated
since:6.1
forRemoval:true
See Also
getIncludePathPatterns(), getExcludePathPatterns()
matchesback to summary
public boolean matches(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:HttpServletRequest

the request to match to

Returns:boolean

true if the interceptor should be applied to the request

matchesback to summary
public boolean matches(String lookupPath, PathMatcher pathMatcher)

Deprecated

since 5.3.

as of 5.3 in favor of matches(HttpServletRequest)

Determine if there is a match for the given lookup path.

Parameters
lookupPath:String

the current request path

pathMatcher:PathMatcher

a path matcher for path pattern matching

Returns:boolean

true if the interceptor applies to the given request path

Annotations
@Deprecated
since:5.3
postHandleback to summary
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception

Overrides default org.springframework.web.servlet.HandlerInterceptor.postHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

Interception 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 org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation is empty.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination

modelAndView:ModelAndView

the ModelAndView that the handler returned (can also be null)

Annotations
@Override
Exceptions
Exception:
in case of errors
preHandleback to summary
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception 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 org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

chosen handler to execute, for type and/or instance evaluation

Returns:boolean

true if 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.

Annotations
@Override
Exceptions
Exception:
in case of errors
setPathMatcherback to summary
public void setPathMatcher(PathMatcher pathMatcher)

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 AntPathMatcher or a custom PathMatcher is required.

By default this is AntPathMatcher.

Note

Setting PathMatcher enforces use of String pattern matching even when a parsed RequestPath is available.

org.springframework.web.servlet.handler back to summary

private Class MappedInterceptor.PatternAdapter

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.MappedInterceptor.PatternAdapter

Contains both the parsed PathPattern and the raw String pattern, and uses the former when the cached path is PathContainer or the latter otherwise. If the pattern cannot be parsed due to unsupported syntax, then PathMatcher is used for all requests.
Since
5.3.6

Field Summary

Modifier and TypeField and Description
private final PathPattern
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
private static PathPattern
public static MappedInterceptor.PatternAdapter[]
public boolean
match(Object path, boolean isPathContainer, PathMatcher pathMatcher)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pathPatternback to summary
private final PathPattern pathPattern
Annotations
@Nullable
patternStringback to summary
private final String patternString

Constructor Detail

PatternAdapterback to summary
public PatternAdapter(String pattern, PathPatternParser parser)

Method Detail

getPatternStringback to summary
public String getPatternString()
initPathPatternback to summary
private static PathPattern initPathPattern(String pattern, PathPatternParser parser)
Annotations
@Nullable
initPatternsback to summary
public static MappedInterceptor.PatternAdapter[] initPatterns(String[] patterns, PathPatternParser parser)
Annotations
@Nullable
matchback to summary
public boolean match(Object path, boolean isPathContainer, PathMatcher pathMatcher)