Spring Boot 映射子文件夹的资源

B. *_*Bri 1 java spring-mvc mapping-resources spring-boot

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

我的资源位于文件夹 resources/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");
   }
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Gui*_*bbe 7

文档中:

\n\n
\n

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

\n
\n\n

所以如果你有:

\n\n
src\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 static\n            \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 images\n            \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 image.png\n            \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
http://localhost:8080/ (index.html)\nhttp://localhost:8080/index.html  \nhttp://localhost:8080/images/image.png  \n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,您不能在 url 中添加 static,并且在添加新资源时必须重新启动服务器。

\n