0

我正在尝试将我的资源的子文件夹映射到服务器 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");
   }
}

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

谢谢

4

2 回答 2

6

文档中:

默认情况下,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  

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

于 2017-06-04T16:57:07.307 回答
0

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

spring.resources.static-locations=classpath:/a/b/c/
于 2020-07-03T21:30:48.960 回答