Class ResourceHttpRequestHandler
- All Implemented Interfaces:
Aware, InitializingBean, ApplicationContextAware, EmbeddedValueResolverAware, ServletContextAware, CorsConfigurationSource, HttpRequestHandler
HttpRequestHandler that serves static resources in an optimized way
according to the guidelines of Page Speed, YSlow, etc.
The properties "locations" and "locationValues" accept locations from which static resources can be served by this handler. This can be relative to the root of the web application, or from the classpath, for example, "classpath:/META-INF/public-web-resources/", allowing convenient packaging and serving of resources such as .js, .css, and others in jar files.
This request handler may also be configured with a
resourcesResolver and
resourceTransformer chains to support
arbitrary resolution and transformation of resources being served. By default,
a PathResourceResolver simply finds resources based on the configured
"locations". An application can configure additional resolvers and transformers
such as the VersionResourceResolver which can resolve and prepare URLs
for resources with a version in the URL.
This handler also properly evaluates the Last-Modified header
(if present) so that a 304 status code will be returned as appropriate,
avoiding unnecessary overhead for resources that are already cached by the client.
- Since:
- 3.0.4
- Author:
- Keith Donald, Jeremy Grelle, Juergen Hoeller, Arjen Poutsma, Brian Clozel, Rossen Stoyanchev
-
Field Summary
Fields inherited from class WebContentGenerator
HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.getCorsConfiguration(jakarta.servlet.http.HttpServletRequest request) Return the specified CORS configuration.Return the HTTP ETag generator function to be used when serving resources.Return the configuredListofResourcelocations including both String-based locations provided viasetLocationValuesand pre-resolvedResourcelocations provided viasetLocations.getMediaType(jakarta.servlet.http.HttpServletRequest request, Resource resource) Determine the media type for the given request and the resource matched to it.Return theconfiguredmedia types.getResource(jakarta.servlet.http.HttpServletRequest request) Return the configured resource converter.Return the configured resource region converter.Return the list of configured resource resolvers.Return the list of configured resource transformers.Deprecated, for removal: This API element is subject to removal in a future version.voidhandleRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Processes a resource request.protected voidLook for aPathResourceResolveramong the configured resource resolvers and set itsallowedLocationsproperty (if empty) to match thelocationsconfigured on this class.protected booleanisInvalidPath(String path) Invoked afterResourceHandlerUtils.isInvalidPath(String)to allow subclasses to perform further validation.booleanReturn 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.booleanReturn whether theResource.lastModified()information is used to drive HTTP responses when serving static resources.protected StringprocessPath(String path) Process the given resource path.voidsetCorsConfiguration(CorsConfiguration corsConfiguration) Specify the CORS configuration for resources served by this handler.voidsetEmbeddedValueResolver(StringValueResolver resolver) Set the StringValueResolver to use for resolving embedded definition values.voidsetEtagGenerator(@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.protected voidsetHeaders(jakarta.servlet.http.HttpServletResponse response, Resource resource, @Nullable MediaType mediaType) Set headers on the given servlet response.voidsetLocations(List<Resource> locations) Configure locations to serve resources from as pre-resourced Resource's.voidsetLocationValues(List<String> locations) Configure String-based locations to serve resources from.voidsetMediaTypes(Map<String, MediaType> mediaTypes) Add mappings between file extensions, extracted from the filename of a staticResource, and corresponding media type to set on the response.voidsetOptimizeLocations(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.voidsetResourceHttpMessageConverter(@Nullable ResourceHttpMessageConverter messageConverter) Configure theResourceHttpMessageConverterto use.voidsetResourceRegionHttpMessageConverter(@Nullable ResourceRegionHttpMessageConverter messageConverter) Configure theResourceRegionHttpMessageConverterto use.voidsetResourceResolvers(@Nullable List<ResourceResolver> resourceResolvers) Configure the list ofResourceResolversto use.voidsetResourceTransformers(@Nullable List<ResourceTransformer> resourceTransformers) Configure the list ofResourceTransformersto use.voidsetUrlPathHelper(@Nullable UrlPathHelper urlPathHelper) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.voidsetUseLastModified(boolean useLastModified) Set whether we should look at theResource.lastModified()when serving resources and use this information to drive"Last-Modified"HTTP response headers.toString()
PathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.