类 VersionResourceResolver
- java.lang.Object
- org.springframework.web.reactive.resource.AbstractResourceResolver
- org.springframework.web.reactive.resource.VersionResourceResolver
- 所有已实现的接口:
ResourceResolver
public class VersionResourceResolver extends AbstractResourceResolver
Resolves request paths containing a version string that can be used as part of an HTTP caching strategy in which a resource is cached with a date in the distant future (e.g. 1 year) and cached until the version, and therefore the URL, is changed.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 theContentVersionStrategy
cannot be combined with JavaScript module loaders. For such cases theFixedVersionStrategy
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.- 从以下版本开始:
- 5.0
- 作者:
- Rossen Stoyanchev, Brian Clozel
- 另请参阅:
VersionStrategy
字段概要
从类继承的字段 org.springframework.web.reactive.resource.AbstractResourceResolver
logger
构造器概要
构造器 构造器 说明 VersionResourceResolver()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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, for example:"{version}/js/main.js"
.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 aVersionStrategy
for the request path of the requested resource.Map<String,VersionStrategy>
getStrategyMap()
Return the map with version strategies keyed by path pattern.protected reactor.core.publisher.Mono<Resource>
resolveResourceInternal(ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain)
protected reactor.core.publisher.Mono<String>
resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain)
void
setStrategyMap(Map<String,VersionStrategy> map)
Set a Map with URL paths as keys andVersionStrategy
as values.从类继承的方法 org.springframework.web.reactive.resource.AbstractResourceResolver
resolveResource, resolveUrlPath
构造器详细资料
VersionResourceResolver
public VersionResourceResolver()
方法详细资料
setStrategyMap
public void setStrategyMap(Map<String,VersionStrategy> map)
Set a Map with URL paths as keys andVersionStrategy
as values.Supports direct URL matches and Ant-style pattern matches. For syntax details, see the
AntPathMatcher
javadoc.- 参数:
map
- a map with URLs as keys and version strategies as values
getStrategyMap
public Map<String,VersionStrategy> getStrategyMap()
Return the map with version strategies keyed by path pattern.
addContentVersionStrategy
public VersionResourceResolver addContentVersionStrategy(String... pathPatterns)
Insert a content-based version in resource URLs that match the given path patterns. The version is computed from the content of the file, e.g."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(java.lang.String, java.lang.String...)
instead for serving JavaScript files.- 参数:
pathPatterns
- one or more resource URL path patterns, relative to the pattern configured with the resource handler- 返回:
- the current instance for chained method invocation
- 另请参阅:
ContentVersionStrategy
addFixedVersionStrategy
public VersionResourceResolver 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"
. 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 (e.g. see
@Value
in Java config).If not done already, variants of the given
pathPatterns
, prefixed with theversion
will be also configured. For example, adding a"/js/**"
path pattern will also cofigure automatically a"/v1.0.0/js/**"
with"v1.0.0"
theversion
String given as an argument.- 参数:
version
- a version stringpathPatterns
- one or more resource URL path patterns, relative to the pattern configured with the resource handler- 返回:
- the current instance for chained method invocation
- 另请参阅:
FixedVersionStrategy
addVersionStrategy
public VersionResourceResolver addVersionStrategy(VersionStrategy strategy, String... pathPatterns)
Register a custom VersionStrategy to apply to resource URLs that match the given path patterns.- 参数:
strategy
- the custom strategypathPatterns
- one or more resource URL path patterns, relative to the pattern configured with the resource handler- 返回:
- the current instance for chained method invocation
- 另请参阅:
VersionStrategy
resolveResourceInternal
protected reactor.core.publisher.Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain)
resolveUrlPathInternal
protected reactor.core.publisher.Mono<String> resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain)
getStrategyForPath
@Nullable protected VersionStrategy getStrategyForPath(String requestPath)
Find aVersionStrategy
for the request path of the requested resource.- 返回:
- an instance of a
VersionStrategy
or null if none matches that request path