I am using Spring boot 1.3.3 My application resources exist under src/main/resources/static example: src/main/resources/static/index.html I am trying to map my static resources with a prefix like /*/resource/**
So that it matches urls like /main/resource/** AND /app/resource/**
When I try that using following code
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/*/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
It comes up with a 404 when I request for domain:8080/app/resource/index.html
But returns requested page when I do domain:8080/index.html (It looked like some default matchers are overriding the ones that I tried to configured.)
And when I use the following code
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
It returns the page domain:8080/app/resource/index.html as expected.
Is something wrong with the ant matchers I am using above? Can I use static resources in the way I want?
Any help is appreciated ..
I had exactly the same problem and found the solution.
According to the 'Spring Boot Cookbook', the ResourceHandlerRegistry uses ant-style patterns. Furthermore I came across ' Spring: Difference of /** and /* with regards to paths ', where Devabc points out that ..
Note that Springs' AntPathMatcher contains bugs: it isn't fully conform the Ant Pattern Style. Example: **/*.css won't work for paths that start with a /, while it should according to Ant Style conventions. – Devabc Jun 12 '15 at 9:52
In order to match urls like http://localhost:8080/frontend/13/style.css , where 13 can vary try this:
/* This did NOT work */
registry.addResourceHandler("/frontend/{id:[0-9]+}/**").addResourceLocations("classpath:/web-app/dist/");
/* This DID work, when removing the preceeding / */
registry.addResourceHandler("frontend/{id:[0-9]+}/**").addResourceLocations("classpath:/web-app/dist/");
So for you this will mean: Try removing the preceding '/' in the url, ie
registry.addResourceHandler("*/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.