我正在使用Spring boot 1.3.3
我的应用程序资源存在于 src / main / resources / static 下
示例: src / main / resources / static / index.html
我试图用我的前缀映射我的静态资源
/ * /资源/ **
这样它就像网址一样匹配
/ main / resource / ** AND
/应用程序/资源/ **
当我尝试使用以下代码时
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 时返回请求的页面 (看起来有些默认匹配器会覆盖我尝试配置的默认匹配器。)
当我使用以下代码时
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
按预期返回页面域:8080 / app / resource / index.html 。
我在上面使用的蚂蚁匹配器有问题吗? 我可以按照我想要的方式使用静态资源吗?
感谢任何帮助..
答案 0 :(得分:0)
我遇到了完全相同的问题并找到了解决方案。
根据'Spring Boot Cookbook',ResourceHandlerRegistry使用ant样式模式。此外,我遇到了'Spring: Difference of /** and /* with regards to paths',Devabc在那里指出......
请注意,Springs的AntPathMatcher包含错误:它不完整 符合Ant图案样式。例: ** / * .css不适用于以/开头的路径,而它应该根据Ant Style 约定。 - Devabc 2015年6月12日9:52
为了匹配http://localhost:8080/frontend/13/style.css等网址,其中13可以变化,请尝试以下方法:
/* 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/");
所以对你来说这意味着:尝试删除url中的前面的'/',即
registry.addResourceHandler("*/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);