2017-01-27 43 views
0

基於Spring Boot tutorial爲動態Web內容提供服務,我希望在Kotlin中也能做到這一點。我的Kotlin項目基於此tutorial。我沒有運行這兩個教程的代碼的問題。使用Spring Boot和Kotlin無法提供動態Web內容

從我的理解,我只需要添加一個控制器,將返回一個模板的引用。

這裏HelloController.kt(位於下的src/main /科特林/富/控制器):

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 /主/資源/模板/ 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") 
} 
+0

在此發佈您的gradle/maven依賴關係。 – Januson

+0

我已經添加了整個文件,因此問題來自於不太明顯的問題(並且我也是全新的漸進式:p) – Flanfl

回答

1

我看起來像spring-boot-starter-web依賴是不夠的,解決設置視圖。嘗試添加spring-boot-starter-thymeleaf依賴項和Thymeleaf應處理您的html文件。

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

你的HTML文件應該在src/main/resources/templates否則可能會被自動檢測到。

+0

的確如此簡單!我添加了依賴性,它工作得很好。非常感謝! – Flanfl

相關問題