【发布时间】:2019-01-07 01:03:20
【问题描述】:
我尝试使用 kotlin 在 Spring Boot 中创建简单的 Hello World 应用程序,但 IntelliJ IDE 向我显示警告说我的控制器类从未使用过,并且指定的端点也无法正常工作。我不知道该怎么处理它。
我使用 Boot Initializr 创建了应用程序,其结构如下所示:
kotlin/
com.myapp.school/
Application.kt
controller/
HelloController.kt
resources/
static/
templates/
hello.html
这是 Application.kt 的代码:
package com.myapp.school
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
最后,我有一个简单的控制器,只有一种方法:
package com.myapp.school.controller
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
@Controller
class HelloController
@GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
转到 localhost:8080/hello 会显示带有 404 状态的 whitelabel 错误页面。我读到 Spring 在启动时将注册的端点打印到控制台中,但我没有找到这样的消息。
谁能告诉我怎么了? 谢谢
【问题讨论】:
-
使用
@RestController而不是@Controller。您当前的控制器将尝试定位名为hello的视图。
标签: spring spring-boot kotlin