0

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/**

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

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.)

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.

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 ..

Vsoma
  • 595
  • 1
  • 5
  • 17
  • See the related questions: http://stackoverflow.com/questions/35632175/how-to-change-the-way-spring-boot-serves-static-files/35632734#35632734 and http://stackoverflow.com/questions/35968479/spring-boot-cannot-find-index-html-under-webapp-folder – Anton N Mar 16 '16 at 03:01
  • Those aren't helpful, as my case is different. I want /{myVar}/resources matcher. myVar is injected at runtime. Thanks for looking into this – Vsoma Mar 16 '16 at 10:48

1 Answers1

0

I had exactly the same problem and found the solution.

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 ..

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

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);
Community
  • 1
  • 1
mararn1618
  • 437
  • 6
  • 16
  • The problem is ResourceHadler considers everything before an * OR ** as a prefix. So, when a pattern starts with an * prefix is empty. – Vsoma Aug 16 '16 at 02:42