B. Bri
B. Bri

Reputation: 585

Spring Boot Mapping Resource of a sub folder

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

Upvotes: 1

Views: 11526

Answers (2)

camel
camel

Reputation: 13

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/

Upvotes: 0

Guillaume Robbe
Guillaume Robbe

Reputation: 676

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.

Upvotes: 6

Related Questions