1

I'm trying to map a sub folder of my resources to server index.html and images associated.

My resources are located inside folder resources/a/b/c. (ie resources/a/b/c/index.html)

I want this html page to be accessible from my root path (http://localhost:8080/index.html).

I'm extending WebMvcConfigurerAdapter to configure the mapping. I tried several path but nothing worked so far.

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter
{
   public static void main(String[] args)
   {
      SpringApplication.run(Application.class, args);
   }

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry)
   {
      registry.addResourceHandler("/**").addResourceLocations(
            "classpath:/resources/a/b/c",
            "classpath:/a/b/c",
            "/resources/a/b/c",
            "/a/b/c",
            "classpath:resources/a/b/c",
            "classpath:a/b/c",
            "resources/a/b/c",
            "a/b/c");
   }
}

Can someone give me some guidance on this?

Thanks

B. Bri
  • 546
  • 2
  • 7
  • 23
  • try adding redirect `@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("index.html", "/a/b/c/index.html"); super.addViewControllers(registry); }` – azl Mar 14 '17 at 14:40

2 Answers2

7

From the documentation:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.

So if you have:

src
└── main
    └── resources
        └── static
            ├── images
            │       └── image.png
            └── index.html

You can access your resources via :

http://localhost:8080/ (index.html)
http://localhost:8080/index.html  
http://localhost:8080/images/image.png  

Notice that you must not add static in the url and must restart the server when adding a new resource.

Guillaume Robbe
  • 658
  • 1
  • 7
  • 18
  • I'll be honest I have the same problem - spring seems happy to server content from 'public' but not from, say, 'public/images' – Andy Sep 14 '18 at 08:57
  • @Andy: Is your URL just the sub-folder without any specification of file? (e.g. `http://localhost/subfolder/`). Spring Boot will only auto-serve an `index.html` file from the top-level folder, e.g. from `src/main/resources/static` in this example, not for a sub-folder. – peterh Jan 27 '19 at 21:10
0

By default Spring Boot servers static content resources under the root path. You can change it by setting spring.resources.static-locations configuration property like this:

spring.resources.static-locations=classpath:/a/b/c/
camel
  • 13
  • 5