Package org.springframework.web.servlet.config.annotation

# Class ResourceHandlerRegistration

java.lang.Object
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration

public class ResourceHandlerRegistration extends Object
Encapsulates information required to create a resource handler.

Since:
  3.1
Author:
  Rossen Stoyanchev, Keith Donald, Brian Clozel

## Constructor Summary

Constructors
Constructor
Description
`ResourceHandlerRegistration(String... pathPatterns)`
Create a `ResourceHandlerRegistration` instance.

## Method Summary

Modifier and Type
Method
Description
`ResourceHandlerRegistration`
`addResourceLocations(String... locations)`
Add one or more resource locations from which to serve static content.
`ResourceHandlerRegistration`
`addResourceLocations(org.springframework.core.io.Resource... 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`
`getRequestHandler()`
Return a `ResourceHttpRequestHandler` instance.
`ResourceChainRegistration`
`resourceChain(boolean cacheResources)`
Configure a chain of resource resolvers and transformers to use.
`ResourceChainRegistration`
`resourceChain(boolean cacheResources, org.springframework.cache.Cache cache)`
Configure a chain of resource resolvers and transformers to use.
`ResourceHandlerRegistration`
`setCacheControl(org.springframework.http.CacheControl cacheControl)`
Specify the `CacheControl` which should be used by the resource handler.
`ResourceHandlerRegistration`
`setCachePeriod(Integer cachePeriod)`
Specify the cache period for the resources served by the resource handler, in seconds.
`ResourceHandlerRegistration`
`setEtagGenerator(Function<org.springframework.core.io.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.
`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.
`ResourceHandlerRegistration`
`setUseLastModified(boolean useLastModified)`
Set whether the `Resource.lastModified()` information should be used to drive HTTP responses.

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

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

## Constructor Details

### ResourceHandlerRegistration

public ResourceHandlerRegistration(String... pathPatterns)
Create a `ResourceHandlerRegistration` instance.

Parameters:
  `pathPatterns` - one or more resource URL path patterns

## Method Details

### addResourceLocations

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` (for example, 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, for example, `[charset=Windows-31J]https://example.org/path`.

Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation

### addResourceLocations

public ResourceHandlerRegistration addResourceLocations(org.springframework.core.io.Resource... locations)
Configure locations to serve static resources from based on pre-resolved `Resource` references.

Parameters:
  `locations` - the resource locations to use
Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  5.3.3

### setCachePeriod

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` - the time to cache resources in seconds
Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation

### setCacheControl

public ResourceHandlerRegistration setCacheControl(org.springframework.http.CacheControl cacheControl)
Specify the `CacheControl` which should be used by the resource handler.

Setting a custom value here will override the configuration set with `setCachePeriod(java.lang.Integer)`.

Parameters:
  `cacheControl` - the CacheControl configuration to use
Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  4.2

### setUseLastModified

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` - whether the "last modified" resource information should be used
Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  5.3
See Also:
  - `ResourceHttpRequestHandler.setUseLastModified(boolean)`

### setEtagGenerator

public ResourceHandlerRegistration setEtagGenerator(@Nullable Function<org.springframework.core.io.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` - the HTTP ETag generator function to use.
Since:
  6.1
See Also:
  - `ResourceHttpRequestHandler.setEtagGenerator(Function)`

### setOptimizeLocations

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` - whether to optimize the locations through an existence check on startup
Returns:
  the same `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  5.3.13
See Also:
  - `ResourceHttpRequestHandler.setOptimizeLocations(boolean)`

### resourceChain

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` - 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 `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  4.1

### resourceChain

public ResourceChainRegistration resourceChain(boolean cacheResources, org.springframework.cache.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` - 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` - the cache to use for storing resolved and transformed resources; by default a `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:
  the same `ResourceHandlerRegistration` instance, for chained method invocation
Since:
  4.1

### getPathPatterns

protected String[] getPathPatterns()
Return the URL path patterns for the resource handler.

### getRequestHandler

protected ResourceHttpRequestHandler getRequestHandler()
Return a `ResourceHttpRequestHandler` instance.