Package org.springframework.web.filter

# Class UrlHandlerFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.UrlHandlerFilter

All Implemented Interfaces:
  `Filter`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.EnvironmentAware`, `org.springframework.core.env.EnvironmentCapable`, `ServletContextAware`

public final class UrlHandlerFilter extends OncePerRequestFilter
`Filter` that modifies the URL, and then redirects or wraps the request to apply the change.

To create an instance, you can use the following:

     UrlHandlerFilter filter = UrlHandlerFilter
        .trailingSlashHandler("/path1/**").redirect(HttpStatus.PERMANENT_REDIRECT)
        .trailingSlashHandler("/path2/**").wrapRequest()
        .build();

This `Filter` should be ordered after `ForwardedHeaderFilter` and before any security filters.

Since:
  6.2
Author:
  Rossen Stoyanchev

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`UrlHandlerFilter.Builder`
Builder for `UrlHandlerFilter`.

## Field Summary

### Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

`ALREADY_FILTERED_SUFFIX`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread.
`protected boolean`
`shouldNotFilterAsyncDispatch()`
The dispatcher type `jakarta.servlet.DispatcherType.ASYNC` means a filter can be invoked in more than one thread over the course of a single request.
`protected boolean`
`shouldNotFilterErrorDispatch()`
Whether to filter error dispatches such as when the servlet container processes and error mapped in `web.xml`.
`static UrlHandlerFilter.Builder.TrailingSlashSpec`
`trailingSlashHandler(String... pathPatterns)`
Create a builder by adding a handler for URL's with a trailing slash.

### Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

`doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter`

### Methods inherited from class org.springframework.web.filter.GenericFilterBean

`addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### shouldNotFilterAsyncDispatch

protected boolean shouldNotFilterAsyncDispatch()
Description copied from class: `OncePerRequestFilter`
The dispatcher type `jakarta.servlet.DispatcherType.ASYNC` means a filter can be invoked in more than one thread over the course of a single request. Some filters only need to filter the initial thread (for example, request wrapping) while others may need to be invoked at least once in each additional thread for example for setting up thread locals or to perform final processing at the very end.

Note that although a filter can be mapped to handle specific dispatcher types via `web.xml` or in Java through the `ServletContext`, servlet containers may enforce different defaults with respect to dispatcher types. This flag enforces the design intent of the filter.

The default return value is "true", which means the filter will not be invoked during subsequent async dispatches. If "false", the filter will be invoked during async dispatches with the same guarantees of being invoked only once during a request within a single thread.

Overrides:
  `shouldNotFilterAsyncDispatch` in class `OncePerRequestFilter`

### shouldNotFilterErrorDispatch

protected boolean shouldNotFilterErrorDispatch()
Description copied from class: `OncePerRequestFilter`
Whether to filter error dispatches such as when the servlet container processes and error mapped in `web.xml`. The default return value is "true", which means the filter will not be invoked in case of an error dispatch.

Overrides:
  `shouldNotFilterErrorDispatch` in class `OncePerRequestFilter`

### doFilterInternal

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException
Description copied from class: `OncePerRequestFilter`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread. See `OncePerRequestFilter.shouldNotFilterAsyncDispatch()` for details.

Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

Specified by:
  `doFilterInternal` in class `OncePerRequestFilter`
Throws:
  `ServletException`
  `IOException`

### trailingSlashHandler

public static UrlHandlerFilter.Builder.TrailingSlashSpec trailingSlashHandler(String... pathPatterns)
Create a builder by adding a handler for URL's with a trailing slash.

Parameters:
  `pathPatterns` - path patterns to map the handler to, for example, `"/path/*"`, `"/path/**"`, `"/path/foo/"`.
Returns:
  a spec to configure the trailing slash handler with
See Also:
  - `UrlHandlerFilter.Builder.trailingSlashHandler(String...)`