使用Spring Boot和Kotlin无法提供动态Web内容

基于Spring Boot 教程来提供动态网页内容,我想在Kotlin中做同样的事情。 我的Kotlin项目是基于这个教程 。 我没有运行这两个教程的代码的问题。

从我的理解,我只需要添加一个控制器,将返回一个模板的引用。

这里HelloController.kt (位于src / main / kotlin / foo / controller下):

 package foo.controller import org.slf4j.LoggerFactory import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping @Controller class HelloController { private val log = LoggerFactory.getLogger(HelloController::class.java) @RequestMapping("/") fun hello(): String { log.info("foo") return "index" } } 

以下是我想要访问的简单“模板” index.html (位于src / main / resources / templates / index.html下):

 <html> <head> </head> <body> Bar </body> </html> 

所以在技术上,如果我去localhost:8080我应该有index.html显示我不。 相反,我有一个404错误。 我有记录显示,所以hello方法被调用。 我究竟做错了什么? 我没有看到Spring Boot教程中的任何配置文件,所以我猜Spring正在做一些事情来获得正确的源自函数返回的资源。

编辑:

已请求我的渐进式进口:

 buildscript { val springBootVersion = "1.4.3.RELEASE" val kotlinVersion = "1.0.6" extra["kotlinVersion"] = kotlinVersion repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion") classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion") classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") } } apply { plugin("kotlin") plugin("kotlin-spring") plugin("kotlin-jpa") plugin("org.springframework.boot") } version = "0.0.1-SNAPSHOT" configure<JavaPluginConvention> { setSourceCompatibility(1.8) setTargetCompatibility(1.8) } repositories { mavenCentral() } val kotlinVersion = extra["kotlinVersion"] as String dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("com.h2database:h2") compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") compile("org.apache.commons:commons-io:1.3.2") compile("org.apache.commons:commons-lang3:3.3.1") testCompile("org.springframework.boot:spring-boot-starter-test") } 

我看起来像spring-boot-starter-web依赖不足以设置视图解析。 尝试添加spring-boot-starter-thymeleaf依赖项,并且Thymeleaf应该处理您的html文件。

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

您的HTML文件应该在src/main/resources/templates ,否则可能不会自动检测到。