如何在Spring-boot中提供静态html内容页面

2022-09-01 16:01:44

我正在通过启动一个嵌入式tomcat,并希望将静态页面作为正在运行的应用程序的一部分。spring-bootindex.html

但以下情况不起作用:

@SpringBootApplication
public class HMyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}


@RestController 
public class HomeContoller {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

src/main/resources/static/index.html

结果:当我调用时,我只看到单词“index”,而不是我的html页面。为什么?localhost:8080


答案 1

我的错:我有一个带有注释的额外类。这不知何故搞砸了弹簧启动自动配置。我删除了它,现在它工作返回。@EnableWebMvcindex.html


答案 2

对我来说,这有效,我相信有更好的方法(比如没有.html)。

@RequestMapping("/")
public String index() {
    return "index.html";
}

推荐