【发布时间】:2017-01-27 15:54:07
【问题描述】:
基于 Spring Boot tutorial 来提供动态 Web 内容,我想在 Kotlin 中做同样的事情。我的 Kotlin 项目基于这个 tutorial。我运行这两个教程的代码都没有问题。
据我了解,我只需要添加一个控制器来返回对模板的引用。
这里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 正在做一些事情来从函数返回的内容中获取正确的资源。
编辑:
已请求我的 graddle 导入:
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")
}
【问题讨论】:
-
在此处发布您的 gradle/maven 依赖项。
-
我已经添加了整个文件,以防问题来自不太明显的问题(而且我对 graddle 也完全陌生:p)
标签: spring spring-mvc spring-boot kotlin