首页 > 解决方案 > Spring Boot:如何更改提供静态文件的路径?

问题描述

我有一个位于路径“C:\Personal Projects\Spring”中的 Spring Boot 项目,我希望它为位于“C:\Personal Projects\Game\build”中的名为 index.html 的浏览器静态 HTML 文件提供服务。

因此,我编写了以下代码:

@SpringBootApplication
@EnableAutoConfiguration
public class Main {
    public static void main(String[] args) throws IOException {
        SpringApplication app = new SpringApplication(Main.class);
        Properties properties = new Properties();
        properties.setProperty("spring.resources.static-locations",
                "C:\\Personal Projects\\Game\\build");
        app.setDefaultProperties(properties);
        app.run(args);
    }
} 

当我运行程序并打开“localhost:8080/index.html”浏览器时,我收到 404 错误。

你知道我做错了什么吗?

标签: spring-boot

解决方案


它应该是:

properties.setProperty("spring.resources.static-locations",
                "file:C:\\Personal Projects\\Game\\build");

推荐阅读