Class PathResourceResolver
java.lang.Object
org.springframework.web.servlet.resource.AbstractResourceResolver
org.springframework.web.servlet.resource.PathResourceResolver
- All Implemented Interfaces:
ResourceResolver
A simple
ResourceResolver
that tries to find a resource under the given
locations matching to the request path.
This resolver does not delegate to the ResourceResolverChain
and is
expected to be configured at the end in a chain of resolvers.
- Since:
- 4.1
- Author:
- Jeremy Grelle, Rossen Stoyanchev, Sam Brannen
-
Field Summary
Fields inherited from class org.springframework.web.servlet.resource.AbstractResourceResolver
logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
checkResource
(Resource resource, Resource location) Perform additional checks on a resolved resource beyond checking whether the resource exists and is readable.Return charsets associated with static resource locations.getResource
(String resourcePath, Resource location) Find the resource under the given location.Deprecated, for removal: This API element is subject to removal in a future version.resolveResourceInternal
(@Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) resolveUrlPathInternal
(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) void
setAllowedLocations
(Resource @Nullable ... locations) By default, when a Resource is found, the path of the resolved resource is compared to ensure it's under the input location where it was found.void
setLocationCharsets
(Map<Resource, Charset> locationCharsets) Configure charsets associated with locations.void
setUrlPathHelper
(@Nullable UrlPathHelper urlPathHelper) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.Methods inherited from class org.springframework.web.servlet.resource.AbstractResourceResolver
resolveResource, resolveUrlPath
-
Constructor Details
-
PathResourceResolver
public PathResourceResolver()
-
-
Method Details
-
setAllowedLocations
By default, when a Resource is found, the path of the resolved resource is compared to ensure it's under the input location where it was found. However sometimes that may not be the case, for example, whenCssLinkResourceTransformer
resolves public URLs of links it contains, the CSS file is the location and the resources being resolved are css files, images, fonts and others located in adjacent or parent directories.This property allows configuring a complete list of locations under which resources must be so that if a resource is not under the location relative to which it was found, this list may be checked as well.
By default
ResourceHttpRequestHandler
initializes this property to match its list of locations.- Parameters:
locations
- the list of allowed locations- Since:
- 4.1.2
- See Also:
-
getAllowedLocations
-
setLocationCharsets
Configure charsets associated with locations. If a static resource is found under aURL resource
location the charset is used to encode the relative pathNote: the charset is used only if the
urlPathHelper
property is also configured and itsurlDecode
property is set to true.- Since:
- 4.3.13
-
getLocationCharsets
Return charsets associated with static resource locations.- Since:
- 4.3.13
-
setUrlPathHelper
@Deprecated(since="7.0", forRemoval=true) public void setUrlPathHelper(@Nullable UrlPathHelper urlPathHelper) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.Provide a reference to theUrlPathHelper
used to map requests to static resources. This helps to derive information about the lookup path such as whether it is decoded or not.- Since:
- 4.3.13
-
getUrlPathHelper
Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.The configuredUrlPathHelper
.- Since:
- 4.3.13
-
resolveResourceInternal
protected @Nullable Resource resolveResourceInternal(@Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) - Specified by:
resolveResourceInternal
in classAbstractResourceResolver
-
resolveUrlPathInternal
protected @Nullable String resolveUrlPathInternal(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) - Specified by:
resolveUrlPathInternal
in classAbstractResourceResolver
-
getResource
Find the resource under the given location.The default implementation checks if there is a readable
Resource
for the given path relative to the location.- Parameters:
resourcePath
- the path to the resourcelocation
- the location to check- Returns:
- the resource, or
null
if none found - Throws:
IOException
-
checkResource
Perform additional checks on a resolved resource beyond checking whether the resource exists and is readable. The default implementation also verifies the resource is either under the location relative to which it was found or is under one of the allowed locations.- Parameters:
resource
- the resource to checklocation
- the location relative to which the resource was found- Returns:
- "true" if resource is in a valid location, "false" otherwise
- Throws:
IOException
- Since:
- 4.1.2
-
PathMatcher
andUrlPathHelper
is deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser
.