类 ResourceWebHandler
- java.lang.Object
- org.springframework.web.reactive.resource.ResourceWebHandler
- 所有已实现的接口:
InitializingBean,WebHandler
public class ResourceWebHandler extends Object implements WebHandler, InitializingBean
HttpRequestHandlerthat serves static resources in an optimized way according to the guidelines of Page Speed, YSlow, etc.The "locations" property takes a list of Spring
Resourcelocations from which static resources are allowed to be served by this handler. Resources could be served from a classpath location, e.g. "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
resourcesResolverandresourceTransformerchains to support arbitrary resolution and transformation of resources being served. By default aPathResourceResolversimply finds resources based on the configured "locations". An application can configure additional resolvers and transformers such as theVersionResourceResolverwhich can resolve and prepare URLs for resources with a version in the URL.This handler also properly evaluates the
Last-Modifiedheader (if present) so that a304status code will be returned as appropriate, avoiding unnecessary overhead for resources that are already cached by the client.- 从以下版本开始:
- 5.0
- 作者:
- Rossen Stoyanchev, Brian Clozel
构造器概要
构造器 构造器 说明 ResourceWebHandler()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidafterPropertiesSet()Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.CacheControlgetCacheControl()Return theCacheControlinstance to build the Cache-Control HTTP response header.List<Resource>getLocations()Return theListofResourcepaths to use as sources for serving static resources.List<String>getLocationValues()Return the configured location values.protected reactor.core.publisher.Mono<Resource>getResource(ServerWebExchange exchange)ResourceHttpMessageWritergetResourceHttpMessageWriter()Return the configured resource message writer.List<ResourceResolver>getResourceResolvers()Return the list of configured resource resolvers.List<ResourceTransformer>getResourceTransformers()Return the list of configured resource transformers.reactor.core.publisher.Mono<Void>handle(ServerWebExchange exchange)Processes a resource request.protected voidinitAllowedLocations()Look for aPathResourceResolveramong the configured resource resolvers and set itsallowedLocationsproperty (if empty) to match thelocationsconfigured on this class.protected booleanisInvalidPath(String path)Identifies invalid resource paths.protected StringprocessPath(String path)Process the given resource path.voidsetCacheControl(CacheControl cacheControl)Set theCacheControlinstance to build the Cache-Control HTTP response header.protected voidsetHeaders(ServerWebExchange exchange, Resource resource, MediaType mediaType)Set headers on the response.voidsetLocations(List<Resource> locations)Set theListofResourcepaths to use as sources for serving static resources.voidsetLocationValues(List<String> locationValues)Accepts a list of String-based location values to be resolved intoResourcelocations.voidsetResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter)Configure theResourceHttpMessageWriterto use.voidsetResourceLoader(ResourceLoader resourceLoader)Provide the ResourceLoader to loadlocation valueswith.voidsetResourceResolvers(List<ResourceResolver> resourceResolvers)Configure the list ofResourceResolversto use.voidsetResourceTransformers(List<ResourceTransformer> resourceTransformers)Configure the list ofResourceTransformersto use.StringtoString()
构造器详细资料
ResourceWebHandler
public ResourceWebHandler()
方法详细资料
setLocationValues
public void setLocationValues(List<String> locationValues)
Accepts a list of String-based location values to be resolved intoResourcelocations.- 从以下版本开始:
- 5.1
getLocationValues
public List<String> getLocationValues()
Return the configured location values.- 从以下版本开始:
- 5.1
setLocations
public void setLocations(@Nullable List<Resource> locations)
Set theListofResourcepaths to use as sources for serving static resources.
getLocations
public List<Resource> getLocations()
Return theListofResourcepaths to use as sources for serving static resources.Note that if
locationValuesare provided, instead of loaded Resource-based locations, this method will return empty until after initialization viaafterPropertiesSet().
setResourceResolvers
public void setResourceResolvers(@Nullable List<ResourceResolver> resourceResolvers)
Configure the list ofResourceResolversto use.By default
PathResourceResolveris configured. If using this property, it is recommended to addPathResourceResolveras the last resolver.
getResourceResolvers
public List<ResourceResolver> getResourceResolvers()
Return the list of configured resource resolvers.
setResourceTransformers
public void setResourceTransformers(@Nullable List<ResourceTransformer> resourceTransformers)
Configure the list ofResourceTransformersto use.By default no transformers are configured for use.
getResourceTransformers
public List<ResourceTransformer> getResourceTransformers()
Return the list of configured resource transformers.
setCacheControl
public void setCacheControl(@Nullable CacheControl cacheControl)
Set theCacheControlinstance to build the Cache-Control HTTP response header.
getCacheControl
@Nullable public CacheControl getCacheControl()
Return theCacheControlinstance to build the Cache-Control HTTP response header.
setResourceHttpMessageWriter
public void setResourceHttpMessageWriter(@Nullable ResourceHttpMessageWriter httpMessageWriter)
Configure theResourceHttpMessageWriterto use.By default a
ResourceHttpMessageWriterwill be configured.
getResourceHttpMessageWriter
@Nullable public ResourceHttpMessageWriter getResourceHttpMessageWriter()
Return the configured resource message writer.
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
Provide the ResourceLoader to loadlocation valueswith.- 从以下版本开始:
- 5.1
afterPropertiesSet
public void afterPropertiesSet() throws Exception
从接口复制的说明:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet在接口中InitializingBean- 抛出:
Exception- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
initAllowedLocations
protected void initAllowedLocations()
Look for aPathResourceResolveramong the configured resource resolvers and set itsallowedLocationsproperty (if empty) to match thelocationsconfigured on this class.
handle
public reactor.core.publisher.Mono<Void> handle(ServerWebExchange exchange)
Processes a resource request.Checks for the existence of the requested resource in the configured list of locations. If the resource does not exist, a
404response will be returned to the client. If the resource exists, the request will be checked for the presence of theLast-Modifiedheader, and its value will be compared against the last-modified timestamp of the given resource, returning a304status code if theLast-Modifiedvalue is greater. If the resource is newer than theLast-Modifiedvalue, or the header is not present, the content resource of the resource will be written to the response with caching headers set to expire one year in the future.- 指定者:
handle在接口中WebHandler- 参数:
exchange- the current server exchange- 返回:
Mono<Void>to indicate when request handling is complete
getResource
protected reactor.core.publisher.Mono<Resource> getResource(ServerWebExchange exchange)
processPath
protected String processPath(String path)
Process the given resource path.The default implementation replaces:
- Backslash with forward slash.
- Duplicate occurrences of slash with a single slash.
- Any combination of leading slash and control characters (00-1F and 7F) with a single "/" or "". For example
" / // foo/bar"becomes"/foo/bar".
- 从以下版本开始:
- 3.2.12
isInvalidPath
protected boolean isInvalidPath(String path)
Identifies invalid resource paths. By default rejects:- Paths that contain "WEB-INF" or "META-INF"
- Paths that contain "../" after a call to
StringUtils.cleanPath(java.lang.String). - Paths that represent a
valid URLor would represent one after the leading slash is removed.
Note: this method assumes that leading, duplicate '/' or control characters (e.g. white space) have been trimmed so that the path starts predictably with a single '/' or does not have one.
- 参数:
path- the path to validate- 返回:
trueif the path is invalid,falseotherwise
setHeaders
protected void setHeaders(ServerWebExchange exchange, Resource resource, @Nullable MediaType mediaType) throws IOException
Set headers on the response. Called for both GET and HEAD requests.- 参数:
exchange- current exchangeresource- the identified resource (nevernull)mediaType- the resource's media type (nevernull)- 抛出:
IOException