public class VersionResourceResolver extends AbstractResourceResolver
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 the
CssLinkResourceTransformer
should also be used in order to modify
links within CSS files to also contain versions.
VersionStrategy
logger
Constructor and Description |
---|
VersionResourceResolver() |
Modifier and Type | Method and Description |
---|---|
VersionResourceResolver |
addContentVersionStrategy(String... pathPatterns)
Insert a content-based version in resource URLs that match the given path
patterns.
|
VersionResourceResolver |
addFixedVersionStrategy(String version,
String... pathPatterns)
Insert a fixed, prefix-based version in resource URLs that match the given
path patterns, e.g.
|
VersionResourceResolver |
addVersionStrategy(VersionStrategy strategy,
String... pathPatterns)
Register a custom VersionStrategy to apply to resource URLs that match the
given path patterns.
|
protected VersionStrategy |
getStrategyForPath(String requestPath)
Find a
VersionStrategy for the request path of the requested resource. |
Map<String,VersionStrategy> |
getStrategyMap()
Return the map with version strategies keyed by path pattern.
|
protected Resource |
resolveResourceInternal(HttpServletRequest request,
String requestPath,
List<? extends Resource> locations,
ResourceResolverChain chain) |
protected String |
resolveUrlPathInternal(String resourceUrlPath,
List<? extends Resource> locations,
ResourceResolverChain chain) |
void |
setStrategyMap(Map<String,VersionStrategy> map)
Set a Map with URL paths as keys and
VersionStrategy as values. |
resolveResource, resolveUrlPath
public void setStrategyMap(Map<String,VersionStrategy> map)
VersionStrategy
as values.
Supports direct URL matches and Ant-style pattern matches. For syntax
details, see the AntPathMatcher
javadoc.
map
- map with URLs as keys and version strategies as valuespublic Map<String,VersionStrategy> getStrategyMap()
public VersionResourceResolver addContentVersionStrategy(String... pathPatterns)
"css/main-e36d2e05253c6c7085a91522ce43a0b4.css"
. This is a good
default strategy to use except when it cannot be, for example when using
JavaScript module loaders, use addFixedVersionStrategy(java.lang.String, java.lang.String...)
instead
for serving JavaScript files.pathPatterns
- one or more resource URL path patternsContentVersionStrategy
public VersionResourceResolver addFixedVersionStrategy(String version, String... pathPatterns)
"{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, fetched from a
git commit sha, a property file, environment variable, and set with SpEL
expressions in the configuration (e.g. see @Value
in Java config).
version
- a version stringpathPatterns
- one or more resource URL path patternsFixedVersionStrategy
public VersionResourceResolver addVersionStrategy(VersionStrategy strategy, String... pathPatterns)
strategy
- the custom strategypathPatterns
- one or more resource URL path patternsVersionStrategy
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain)
resolveResourceInternal
in class AbstractResourceResolver
protected String resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain)
resolveUrlPathInternal
in class AbstractResourceResolver
protected VersionStrategy getStrategyForPath(String requestPath)
VersionStrategy
for the request path of the requested resource.VersionStrategy
or null if none matches that request path