gpt4 book ai didi

java - Spring Boot 映射子文件夹的资源

转载 作者:行者123 更新时间:2023-12-02 03:03:14 24 4
gpt4 key购买 nike

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

我的资源位于文件夹 resources/a/b/c 内。 (即resources/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");
}
}

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

谢谢

最佳答案

来自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.

所以如果你有:

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中不能添加static,添加新资源时必须重启服务器。

关于java - Spring Boot 映射子文件夹的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com