Spring Boot静态资源与ResourceHandlerRegistry映射蚂蚁匹配器 [英] Spring boot static resources mapping ant matchers with ResourceHandlerRegistry
问题描述
我正在使用Spring Boot 1.3.3
我的应用程序资源位于 src/main/resources/static 下
示例: src/main/resources/static/index.html
我正在尝试使用诸如这样的前缀来映射我的静态资源
/*/资源/**
这样它就可以匹配网址
/main/resource/** AND
/app/resource/**
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/**
当我尝试使用以下代码
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);
}
当我请求域时,它带有404:8080/app/resource/index.html
但是当我执行 domain:8080/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.)
当我使用以下代码时
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
它将按预期返回页面域:8080/app/resource/index.html .
我在上面使用的蚂蚁匹配器有问题吗? 我可以按照自己的方式使用静态资源吗?
Is something wrong with the ant matchers I am using above? Can I use static resources in the way I want?
感谢您的帮助.
推荐答案
我遇到了完全相同的问题,并找到了解决方案.
I had exactly the same problem and found the solution.
根据"Spring Boot Cookbook",ResourceHandlerRegistry使用蚂蚁风格的模式.此外,我遇到了"春季:/**和/*与关于路径',Devabc指出了..
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 ..
请注意,Springs的AntPathMatcher包含错误:它并不完全 符合蚂蚁图案样式.例子: **/*.css不适用于以/开头的路径,但它应该根据Ant样式 约定. – Devabc 15年6月12日,9:52
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
为了匹配诸如 http://localhost:8080/frontend/13/style .css ,其中13个可以变化,请尝试以下操作:
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, i.e.
registry.addResourceHandler("*/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
这篇关于Spring Boot静态资源与ResourceHandlerRegistry映射蚂蚁匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!