简体   繁体   English

Spring Boot静态资源与ResourceHandlerRegistry映射蚂蚁匹配器

[英]Spring boot static resources mapping ant matchers with ResourceHandlerRegistry

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/** 我正在使用Spring Boot 1.3.3,我的应用程序资源位于src / main / resources / static示例下: src / main / resources / static / index.html我正在尝试使用/ * / resource /这样的前缀映射我的静态资源**
So that it matches urls like /main/resource/** AND /app/resource/** 这样就可以匹配/ main / resource / **和/ 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 当我请求domain:8080 / app / resource / index.html时,它带有404

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.) 但是当我执行domain:8080 / index.html时,返回请求的页面(看起来有些默认匹配器覆盖了我尝试配置的匹配器。)

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. 它将按预期返回页面域: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? 我可以按照自己的方式使用静态资源吗?

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. 根据“ Spring Boot Cookbook”的介绍,ResourceHandlerRegistry使用蚂蚁风格的模式。 Furthermore I came across ' Spring: Difference of /** and /* with regards to paths ', where Devabc points out that .. 此外,我遇到了“ Spring:关于路径的/ **和/ *的区别 ”,Devabc指出了..

Note that Springs' AntPathMatcher contains bugs: it isn't fully conform the Ant Pattern Style. 请注意,Springs的AntPathMatcher包含一些错误:它并不完全符合Ant Pattern Style。 Example: **/*.css won't work for paths that start with a /, while it should according to Ant Style conventions. 示例:** / *。css不适用于以/开头的路径,尽管它应遵循Ant样式约定。 – Devabc Jun 12 '15 at 9:52 – Devabc 15年6月12日在9:52

In order to match urls like http://localhost:8080/frontend/13/style.css , where 13 can vary try this: 为了匹配诸如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/");

So for you this will mean: Try removing the preceding '/' in the url, ie 因此,对您而言,这意味着:尝试在url中删除前面的“ /”,即

registry.addResourceHandler("*/resource/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM