Spring Boot Mapping子文件夹的资源

时间:2017-02-10 10:37:05

标签: java spring-mvc spring-boot mapping-resources

我正在尝试将资源的子文件夹映射到服务器index.html和相关的图像。

我的资源位于文件夹资源/ a / b / c中。 (即资源/ a / b / c / index.html)

我希望可以从我的根路径(http://localhost:8080/index.html)访问此html页面。

我正在扩展WebMvcConfigurerAdapter来配置映射。我尝试了几条路,但到目前为止没有任何工作。

@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");
   }
}

有人可以给我一些指导吗?

由于

2 个答案:

答案 0 :(得分:2)

来自documentation

  

默认情况下,Spring Boot将提供目录中的静态内容   在/中调用/ static(或/ public或/ resources或/ META-INF / resources)   类路径或来自ServletContext的根目录。

所以如果你有:

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

您可以通过以下方式访问您的资源:

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

请注意,您不得在网址中添加静态内容,并且必须在添加新资源时重新启动服务器。

答案 1 :(得分:0)

默认情况下,Spring Boot服务器在根路径下提供静态内容资源。 您可以通过设置 spring.resources.static-locations 配置属性来更改它,如下所示:

spring.resources.static-locations=classpath:/a/b/c/