Java Spring boot静态资源映射ant matchers与ResourceHandlerRegistry
我使用的是Spring boot 1.3.3 我的应用程序资源位于src/main/resources/static下 示例:src/main/resources/static/index.html 我试图用前缀映射我的静态资源,如 /*/资源/**Java Spring boot静态资源映射ant matchers与ResourceHandlerRegistry,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我使用的是Spring boot 1.3.3 我的应用程序资源位于src/main/resources/static下 示例:src/main/resources/static/index.html 我试图用前缀映射我的静态资源,如 /*/资源/** 这样它就可以匹配URL,比如 /主要/资源/**和 /应用程序/资源/** 当我尝试使用以下代码时 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "cl
这样它就可以匹配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);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
当我请求域:8080/app/resource/index.html时,它会出现一个404
但当我执行域:8080/index.html时返回请求的页面
(看起来有些默认匹配器正在覆盖我尝试配置的匹配器。)
当我使用下面的代码时
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);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
按预期返回页面域:8080/app/resource/index.html
我上面使用的蚂蚁匹配器有什么问题吗?
我可以用我想要的方式使用静态资源吗
非常感谢您的帮助。我遇到了完全相同的问题,并找到了解决方案 根据“SpringBoot食谱”,ResourceHandlerRegistry使用ant样式的模式。此外,我还遇到了“”,Devabc指出 请注意,Springs的AntPathMatcher包含bug:它不是完全匹配的 符合蚂蚁图案风格。例子: **/*.css不适用于以/开头的路径,但它应该符合Ant样式 公约–Devabc 6月12日15时9分52分 为了匹配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);
请看相关问题:这些问题没有帮助,因为我的情况不同。我需要/{myVar}/resources匹配器。myVar在运行时被注入。感谢您对此进行研究。问题是ResourceHadler将*或**之前的所有内容都视为前缀。因此,当一个模式以*前缀开头时,它是空的。