Top Description Fields Constructors Methods
org.springframework.web.servlet.config.annotation

public Class ResourceHandlerRegistration

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .List, java.util.function.Function, org.springframework.cache.Cache, org.springframework.core.io.Resource, org.springframework.http.CacheControl, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.server.ServerWebExchange, org.springframework.web.servlet.resource.PathResourceResolver, .ResourceHttpRequestHandler

Encapsulates information required to create a resource handler.
Authors
Rossen Stoyanchev, Keith Donald, Brian Clozel
Since
3.1

Field Summary

Modifier and TypeField and Description
private CacheControl
private Integer
private Function<Resource, String>
private final List<Resource>
private final List<String>
private boolean
private final String[]
private ResourceChainRegistration
private boolean

Constructor Summary

AccessConstructor and Description
public
ResourceHandlerRegistration(String...
one or more resource URL path patterns
pathPatterns
)

Create a ResourceHandlerRegistration instance.

Method Summary

Modifier and TypeMethod and Description
public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
addResourceLocations
(String... locations)

Add one or more resource locations from which to serve static content.

public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
addResourceLocations
(Resource...
the resource locations to use
locations
)

Configure locations to serve static resources from based on pre-resolved Resource references.

protected String[]
getPathPatterns()

Return the URL path patterns for the resource handler.

protected ResourceHttpRequestHandler
public ResourceChainRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
resourceChain
(boolean
whether to cache the result of resource resolution; setting this to "true" is recommended for production (and "false" for development, especially when applying a version strategy)
cacheResources
)

Configure a chain of resource resolvers and transformers to use.

public ResourceChainRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
resourceChain
(boolean
whether to cache the result of resource resolution; setting this to "true" is recommended for production (and "false" for development, especially when applying a version strategy
cacheResources
,
Cache
the cache to use for storing resolved and transformed resources; by default a org.springframework.cache.concurrent.ConcurrentMapCache is used. Since Resources aren't serializable and can be dependent on the application host, one should not use a distributed cache but rather an in-memory cache.
cache
)

Configure a chain of resource resolvers and transformers to use.

public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
setCacheControl
(CacheControl
the CacheControl configuration to use
cacheControl
)

Specify the org.springframework.http.CacheControl which should be used by the resource handler.

public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
setCachePeriod
(Integer
the time to cache resources in seconds
cachePeriod
)

Specify the cache period for the resources served by the resource handler, in seconds.

public ResourceHandlerRegistration
setEtagGenerator(Function<Resource, String>
the HTTP ETag generator function to use.
etagGenerator
)

Configure a generator function that will be used to create the ETag information, given a Resource that is about to be written to the response.

public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
setOptimizeLocations
(boolean
whether to optimize the locations through an existence check on startup
optimizeLocations
)

Set whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access.

public ResourceHandlerRegistration

Returns:

the same ResourceHandlerRegistration instance, for chained method invocation
setUseLastModified
(boolean
whether the "last modified" resource information should be used
useLastModified
)

Set whether the Resource#lastModified() information should be used to drive HTTP responses.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheControlback to summary
private CacheControl cacheControl
Annotations
@Nullable
cachePeriodback to summary
private Integer cachePeriod
Annotations
@Nullable
etagGeneratorback to summary
private Function<Resource, String> etagGenerator
Annotations
@Nullable
locationsResourcesback to summary
private final List<Resource> locationsResources
locationValuesback to summary
private final List<String> locationValues
optimizeLocationsback to summary
private boolean optimizeLocations
pathPatternsback to summary
private final String[] pathPatterns
resourceChainRegistrationback to summary
private ResourceChainRegistration resourceChainRegistration
Annotations
@Nullable
useLastModifiedback to summary
private boolean useLastModified

Constructor Detail

ResourceHandlerRegistrationback to summary
public ResourceHandlerRegistration(String... pathPatterns)

Create a ResourceHandlerRegistration instance.

Parameters
pathPatterns:String[]

one or more resource URL path patterns

Method Detail

addResourceLocationsback to summary
public ResourceHandlerRegistration addResourceLocations(String... locations)

Add one or more resource locations from which to serve static content. Each location must point to a valid directory. Multiple locations may be specified as a comma-separated list, and the locations will be checked for a given resource in the order specified.

For example, {"/", "classpath:/META-INF/public-web-resources/"} allows resources to be served both from the web application root and from any JAR on the classpath that contains a /META-INF/public-web-resources/ directory, with resources in the web application root taking precedence.

For URL-based resources (e.g. files, HTTP URLs, etc) this method supports a special prefix to indicate the charset associated with the URL so that relative paths appended to it can be encoded correctly, e.g. [charset=Windows-31J]https://example.org/path.

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

addResourceLocationsback to summary
public ResourceHandlerRegistration addResourceLocations(Resource... locations)

Configure locations to serve static resources from based on pre-resolved Resource references.

Parameters
locations:Resource[]

the resource locations to use

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
5.3.3
getPathPatternsback to summary
protected String[] getPathPatterns()

Return the URL path patterns for the resource handler.

getRequestHandlerback to summary
protected ResourceHttpRequestHandler getRequestHandler()

Return a ResourceHttpRequestHandler instance.

resourceChainback to summary
public ResourceChainRegistration resourceChain(boolean cacheResources)

Configure a chain of resource resolvers and transformers to use. This can be useful, for example, to apply a version strategy to resource URLs.

If this method is not invoked, by default only a simple PathResourceResolver is used in order to match URL paths to resources under the configured locations.

Parameters
cacheResources:boolean

whether to cache the result of resource resolution; setting this to "true" is recommended for production (and "false" for development, especially when applying a version strategy)

Returns:ResourceChainRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
4.1
resourceChainback to summary
public ResourceChainRegistration resourceChain(boolean cacheResources, Cache cache)

Configure a chain of resource resolvers and transformers to use. This can be useful, for example, to apply a version strategy to resource URLs.

If this method is not invoked, by default only a simple PathResourceResolver is used in order to match URL paths to resources under the configured locations.

Parameters
cacheResources:boolean

whether to cache the result of resource resolution; setting this to "true" is recommended for production (and "false" for development, especially when applying a version strategy

cache:Cache

the cache to use for storing resolved and transformed resources; by default a org.springframework.cache.concurrent.ConcurrentMapCache is used. Since Resources aren't serializable and can be dependent on the application host, one should not use a distributed cache but rather an in-memory cache.

Returns:ResourceChainRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
4.1
setCacheControlback to summary
public ResourceHandlerRegistration setCacheControl(CacheControl cacheControl)

Specify the org.springframework.http.CacheControl which should be used by the resource handler.

Setting a custom value here will override the configuration set with setCachePeriod.

Parameters
cacheControl:CacheControl

the CacheControl configuration to use

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
4.2
setCachePeriodback to summary
public ResourceHandlerRegistration setCachePeriod(Integer cachePeriod)

Specify the cache period for the resources served by the resource handler, in seconds. The default is to not send any cache headers but to rely on last-modified timestamps only. Set to 0 in order to send cache headers that prevent caching, or to a positive number of seconds to send cache headers with the given max-age value.

Parameters
cachePeriod:Integer

the time to cache resources in seconds

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

setEtagGeneratorback to summary
public ResourceHandlerRegistration setEtagGenerator(Function<Resource, String> etagGenerator)

Configure a generator function that will be used to create the ETag information, given a Resource that is about to be written to the response.

This function should return a String that will be used as an argument in ServerWebExchange#checkNotModified(String), or null if no value can be generated for the given resource.

Parameters
etagGenerator:Function<Resource, String>

the HTTP ETag generator function to use.

Since
6.1
See Also
ResourceHttpRequestHandler#setEtagGenerator(Function)
setOptimizeLocationsback to summary
public ResourceHandlerRegistration setOptimizeLocations(boolean optimizeLocations)

Set whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access.

The default is false, for defensiveness against zip files without directory entries which are unable to expose the existence of a directory upfront. Switch this flag to true for optimized access in case of a consistent jar layout with directory entries.

Parameters
optimizeLocations:boolean

whether to optimize the locations through an existence check on startup

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
5.3.13
See Also
ResourceHttpRequestHandler#setOptimizeLocations
setUseLastModifiedback to summary
public ResourceHandlerRegistration setUseLastModified(boolean useLastModified)

Set whether the Resource#lastModified() information should be used to drive HTTP responses.

This configuration is set to true by default.

Parameters
useLastModified:boolean

whether the "last modified" resource information should be used

Returns:ResourceHandlerRegistration

the same ResourceHandlerRegistration instance, for chained method invocation

Since
5.3
See Also
ResourceHttpRequestHandler#setUseLastModified