Class VersionResourceResolver
- All Implemented Interfaces:
- ResourceResolver
Different versioning strategies exist, and this resolver must be configured with one or more such strategies along with path mappings to indicate which strategy applies to which resources.
ContentVersionStrategy is a good default choice except in cases
where it cannot be used. Most notably the ContentVersionStrategy
cannot be combined with JavaScript module loaders. For such cases the
FixedVersionStrategy is a better choice.
Note that using this resolver to serve CSS files means that the
CssLinkResourceTransformer should also be used in order to modify
links within CSS files to also contain the appropriate versions generated
by this resolver.
- Since:
- 4.1
- Author:
- Brian Clozel, Rossen Stoyanchev
- See Also:
- 
Field SummaryFields inherited from class AbstractResourceResolverlogger
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionaddContentVersionStrategy(String... pathPatterns) Insert a content-based version in resource URLs that match the given path patterns.addFixedVersionStrategy(String version, String... pathPatterns) Insert a fixed, prefix-based version in resource URLs that match the given path patterns, for example:"{version}/js/main.js".addVersionStrategy(VersionStrategy strategy, String... pathPatterns) Register a custom VersionStrategy to apply to resource URLs that match the given path patterns.protected @Nullable VersionStrategygetStrategyForPath(String requestPath) Find aVersionStrategyfor the request path of the requested resource.Return the map with version strategies keyed by path pattern.resolveResourceInternal(@Nullable jakarta.servlet.http.HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) voidsetStrategyMap(Map<String, VersionStrategy> map) Set a Map with URL paths as keys andVersionStrategyas values.Methods inherited from class AbstractResourceResolverresolveResource, resolveUrlPath
- 
Constructor Details- 
VersionResourceResolverpublic VersionResourceResolver()
 
- 
- 
Method Details- 
setStrategyMapSet a Map with URL paths as keys andVersionStrategyas values.Supports direct URL matches and Ant-style pattern matches. For syntax details, see the AntPathMatcherjavadoc.- Parameters:
- map- a map with URLs as keys and version strategies as values
 
- 
getStrategyMapReturn the map with version strategies keyed by path pattern.
- 
addContentVersionStrategyInsert a content-based version in resource URLs that match the given path patterns. The version is computed from the content of the file, for example,"css/main-e36d2e05253c6c7085a91522ce43a0b4.css". This is a good default strategy to use except when it cannot be, for example when using JavaScript module loaders, useaddFixedVersionStrategy(String, String...)instead for serving JavaScript files.- Parameters:
- pathPatterns- one or more resource URL path patterns, relative to the pattern configured with the resource handler
- Returns:
- the current instance for chained method invocation
- See Also:
 
- 
addFixedVersionStrategyInsert a fixed, prefix-based version in resource URLs that match the given path patterns, for example:"{version}/js/main.js". This is useful (vs. content-based versions) when using JavaScript module loaders.The version may be a random number, the current date, or a value fetched from a git commit sha, a property file, or environment variable and set with SpEL expressions in the configuration (for example, see @Valuein Java config).If not done already, variants of the given pathPatterns, prefixed with theversionwill be also configured. For example, adding a"/js/**"path pattern will also configure automatically a"/v1.0.0/js/**"with"v1.0.0"theversionString given as an argument.- Parameters:
- version- a version string
- pathPatterns- one or more resource URL path patterns, relative to the pattern configured with the resource handler
- Returns:
- the current instance for chained method invocation
- See Also:
 
- 
addVersionStrategyRegister a custom VersionStrategy to apply to resource URLs that match the given path patterns.- Parameters:
- strategy- the custom strategy
- pathPatterns- one or more resource URL path patterns, relative to the pattern configured with the resource handler
- Returns:
- the current instance for chained method invocation
- See Also:
 
- 
resolveResourceInternalprotected @Nullable Resource resolveResourceInternal(@Nullable jakarta.servlet.http.HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) - Specified by:
- resolveResourceInternalin class- AbstractResourceResolver
 
- 
resolveUrlPathInternalprotected @Nullable String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) - Specified by:
- resolveUrlPathInternalin class- AbstractResourceResolver
 
- 
getStrategyForPathFind aVersionStrategyfor the request path of the requested resource.- Returns:
- an instance of a VersionStrategyor null if none matches that request path
 
 
-