Reputation: 9791
I have a @Configuration
class that extends WebMvcConfigurerAdapter
with a method similar to this:
@Override
public void addResourceHandlers(ResourceHandlerRegistry r) {
final Integer CACHE_LONGTERM = 31536000;
r.addResourceHandler("jQuery-File-Upload*/**").addResourceLocations("/jQuery-File-Upload-9.9.3").setCachePeriod(CACHE_LONGTERM);
}
When trying to access those static resources Spring does not match the URL (e.g. http://localhost:8080/app/jQuery-File-Upload-9.9.3/js/file.js) with the configured resource handler (404 error or similar). If I change the name of the directory on the filesystem though and remove the initial dashes from the pattern, then it works (e.g. using http://localhost:8080/app/jQueryFileUpload-9.9.3/js/file.js):
@Override
public void addResourceHandlers(ResourceHandlerRegistry r) {
final Integer CACHE_LONGTERM = 31536000;
r.addResourceHandler("jQueryFileUpload*/**").addResourceLocations("/jQueryFileUpload-9.9.3").setCachePeriod(CACHE_LONGTERM);
}
I've tried to debug this a little bit and can see Spring uses org.springframework.util.AntPathMatcher
in order to process these patterns. The code in that class is pretty messy though and I know Spring has had pattern/path related bugs in the past. Is this another defect? How can I modify the code above so it works without having to remove the dashes as I did in the workaround?
Using Spring 4.1.6 and Java 8.
UPDATE
A deleted response suggested "escaping" the dashes somehow. Note, that the following does not work either:
@Override
public void addResourceHandlers(ResourceHandlerRegistry r) {
final Integer CACHE_LONGTERM = 31536000;
r.addResourceHandler("jQuery\\-File\\-Upload*/**").addResourceLocations("/jQuery-File-Upload-9.9.3").setCachePeriod(CACHE_LONGTERM);
}
Upvotes: 3
Views: 1955
Reputation: 59141
Given the following configuration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry r) {
r.addResourceHandler("/resources/foo-*/**").addResourceLocations("/static/");
}
a request like GET /app/resources/foo-bar/file.js
will try to resolve the following on disk: /static/foo-bar/file.js
. Spring is getting the pattern part of the request (given the pattern you've configured) - see AntPathMatcher.extractPathWithinPattern.
So in your case I think it's actually trying to resolve "/jQuery-File-Upload-9.9.3/jQuery-File-Upload-9.9.3/js/file.js"
.
I've managed to resolve resources with a "-"
within the pattern definition.
For additional guidance, turning the LOG level to DEBUG for org.springframework.web.servlet.resource
should give us more information.
If you manage to isolate and reproduce this issue, please create a repro project and/or a JIRA issue
Upvotes: 1