简体   繁体   中英

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

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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