Spring MVC path pattern matching does not handle dashes
I have a class @Configuration
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 these static resources, Spring does not match the URL (e.g. http: // localhost: 8080 / app / jQuery-File-Upload-9.9.3 / js / file.js ) with a resource handler configured (404 error or similar) ... If I change the name of a directory on the filesystem and remove the initial strokes from the template, then it works (for example 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 tried to debug this a bit and can see what Spring is using org.springframework.util.AntPathMatcher
to handle these patterns. The code in this class is pretty messy and I know Spring has had pattern / track bugs in the past. Is this another defect? How do I modify the code above to make it work without having to remove the dash like there was in the workaround?
Using Spring 4.1.6 and Java 8.
UPDATE
The deleted answer suggested somehow "escaping" the dash. Please note that the following does not work:
@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);
}
source to share
Given the following configuration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry r) {
r.addResourceHandler("/resources/foo-*/**").addResourceLocations("/static/");
}
request type GET /app/resources/foo-bar/file.js
will attempt to resolve the following disk space: /static/foo-bar/file.js
. Spring gets the template part of the request (given the template you configured) - see AntPathMatcher.extractPathWithinPattern .
So in your case I think what is actually trying to resolve "/jQuery-File-Upload-9.9.3/jQuery-File-Upload-9.9.3/js/file.js"
.
I was able to resolve resources using "-"
in the template definition.
For more guidance, turning the LOG level on DEBUG for org.springframework.web.servlet.resource
should provide us with more information.
If you can manage to isolate and reproduce this issue, create a replay project and / or JIRA Issue
source to share