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

mem*_*und 24 java spring spring-mvc spring-boot

我正在启动嵌入式tomcat via,spring-boot并希望将静态index.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
Run Code Online (Sandbox Code Playgroud)

结果:当我打电话时localhost:8080,我只看到"索引"这个词,但不是我的html页面.为什么?

mem*_*und 16

我的错:我有一个带@EnableWebMvc注释的附加类.这在某种程度上搞砸了spring-boot自动配置.我删除它,现在它返回工作index.html.


小智 9

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

@RequestMapping("/")
public String index() {
    return "index.html";
}
Run Code Online (Sandbox Code Playgroud)