Class ResourceHandlerRegistration
- Since:
- 5.0
- Author:
- Rossen Stoyanchev
-
Constructor Summary
ConstructorsConstructorDescriptionResourceHandlerRegistration(ResourceLoader resourceLoader, String... pathPatterns) Create aResourceHandlerRegistrationinstance. -
Method Summary
Modifier and TypeMethodDescriptionaddResourceLocations(String... resourceLocations) Add one or more resource locations from which to serve static content.protected String[]Returns the URL path patterns for the resource handler.protected ResourceWebHandlerReturns aResourceWebHandlerinstance.resourceChain(boolean cacheResources) Configure a chain of resource resolvers and transformers to use.resourceChain(boolean cacheResources, Cache cache) Configure a chain of resource resolvers and transformers to use.setCacheControl(CacheControl cacheControl) Specify theCacheControlwhich should be used by the resource handler.setEtagGenerator(@Nullable Function<Resource, String> etagGenerator) Configure a generator function that will be used to create the ETag information, given aResourcethat is about to be written to the response.voidsetMediaTypes(Map<String, MediaType> mediaTypes) Add mappings between file extensions extracted from the filename of staticResources and the media types to use for the response.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.setUseLastModified(boolean useLastModified) Set whether theResource.lastModified()information should be used to drive HTTP responses.
-
Constructor Details
-
ResourceHandlerRegistration
Create aResourceHandlerRegistrationinstance.- Parameters:
resourceLoader- a resource loader for turning a String location into aResourcepathPatterns- one or more resource URL path patterns
-
-
Method Details
-
addResourceLocations
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.- Returns:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation
-
setCacheControl
Specify theCacheControlwhich should be used by the resource handler.- Parameters:
cacheControl- the CacheControl configuration to use- Returns:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation
-
setUseLastModified
Set whether theResource.lastModified()information should be used to drive HTTP responses.This configuration is set to
trueby default.- Parameters:
useLastModified- whether the "last modified" resource information should be used- Returns:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation - Since:
- 5.3
- See Also:
-
setEtagGenerator
public ResourceHandlerRegistration setEtagGenerator(@Nullable Function<Resource, String> etagGenerator) Configure a generator function that will be used to create the ETag information, given aResourcethat 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), ornullif no value can be generated for the given resource.- Parameters:
etagGenerator- the HTTP ETag generator function to use.- Since:
- 6.1
- See Also:
-
setOptimizeLocations
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 totruefor optimized access in case of a consistent jar layout with directory entries.- Parameters:
optimizeLocations- whether to optimize the locations through an existence check on startup- Returns:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation - Since:
- 5.3.13
- See Also:
-
resourceChain
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
PathResourceResolveris used in order to match URL paths to resources under the configured locations.- Parameters:
cacheResources- 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:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation
-
resourceChain
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
PathResourceResolveris used in order to match URL paths to resources under the configured locations.- Parameters:
cacheResources- 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 strategycache- the cache to use for storing resolved and transformed resources; by default aConcurrentMapCacheis 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:
- the same
ResourceHandlerRegistrationinstance, for chained method invocation
-
setMediaTypes
Add mappings between file extensions extracted from the filename of staticResources and the media types to use for the response.Use of this method is typically not necessary since mappings can be also determined via
MediaTypeFactory.getMediaType(Resource).- Parameters:
mediaTypes- media type mappings- Since:
- 5.3.2
-
getPathPatterns
Returns the URL path patterns for the resource handler. -
getRequestHandler
Returns aResourceWebHandlerinstance.
-