Spring引导静态资源使用ResourceHandlerRegistry映射ant匹配器

我正在使用Spring启动1.3.3我的应用程序资源存在于src / main / resources / static示例下: src / main / resources / static / index.html我试图用像/ * / resource /这样的前缀映射我的静态资源**
所以它匹配像/ main / resource / **和/ app / resource / **这样的url

当我尝试使用以下代码时

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

但是当我执行时返回请求页面:8080 / index.html (看起来某些默认匹配器覆盖了我尝试配置的那些。)

当我使用以下代码时

 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS); } 

它返回页面域:8080 / app / resource / index.html按预期方式。

我上面使用的ant匹配器有问题吗? 我可以按照我想要的方式使用静态资源吗?

任何帮助表示赞赏..

我有完全相同的问题,并找到了解决方案。

根据’Spring Boot Cookbook’,ResourceHandlerRegistry使用ant样式模式。 此外,我遇到了’ Spring:/ **和/ *关于路径的差异 ‘,Devabc指出……

请注意,Springs的AntPathMatcher包含错误:它不完全符合Ant模式样式。 示例:** / * .css不适用于以/开头的路径,而应根据Ant样式约定。 – Devabc 2015年6月12日9:52

为了匹配像http:// localhost:8080 / frontend / 13 / style.css这样的url ,其中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);