Spring MVC 如何配置spring Boot 从controller返回index.html?

mum43rcc  于 2023-10-24  发布在  Spring
关注(0)|答案(1)|浏览(136)

我有以下控制器:

@Controller
class ReactAppController {
    @RequestMapping(value = ["/", "/{x:[\\w\\-]+}", "/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}"])
    fun getIndex(): String {
        return "index"
    }
}

我期望它应该返回index.html文件
它的工作原理,如果我把文件index.html下的src/main/resources/templates和应用程序有以下依赖关系:

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")

但我不想

implementation("org.springframework.boot:spring-boot-starter-thymeleaf")

在我的应用程序中,因为我不使用thymeleaf。但如果我删除此依赖-我收到whiteble错误页面。
我认为我必须以某种方式配置viewResolver,但我找不到特定的配方。有办法解决吗?

附言

这不起作用:

spring:
  mvc:
    view:
      prefix: /templates/
      suffix: .html

虽然它看起来是正确的:

aoyhnmkz

aoyhnmkz1#

我用来呈现网页的是:

@RestController
public class WebController {

  @GetMapping("/")
  public Resource homePage() {
    return new ClassPathResource("html/index.html");
  }

你不必在特定的目录下有任何东西。只要在调用ClassPathResource构造函数时指定你的路径。

相关问题