控制器未在我的Spring启动应用程序中执行
我试图使用kotlin在Spring Boot中创建简单的Hello World应用程序,但IntelliJ IDE向我发出警告,我的控制器类从未被使用,指定的端点也无法正常工作。我不知道该怎么处理它 我使用启动初始值设定项创建了应用程序,其结构如下所示:控制器未在我的Spring启动应用程序中执行,spring,spring-boot,kotlin,Spring,Spring Boot,Kotlin,我试图使用kotlin在Spring Boot中创建简单的Hello World应用程序,但IntelliJ IDE向我发出警告,我的控制器类从未被使用,指定的端点也无法正常工作。我不知道该怎么处理它 我使用启动初始值设定项创建了应用程序,其结构如下所示: kotlin/ com.myapp.school/ Application.kt controller/ HelloController.kt resources/ st
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)
}
转到localhost:8080/hello显示状态为404的白标签错误页面。我读到Spring在启动时将注册的端点打印到控制台中,但我没有发现这样的消息
谁能告诉我怎么了?
谢谢你我想你的问题是你有一个没有主体的顶级类(
HelloController
)和一个顶级函数(hello
)。您必须使用大括号来确保hello
是HelloController
的成员
你有这个:
@Controller
class HelloController
@GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
它需要是这样的,因此hello
属于HelloController
,而不是在同一级别:
@Controller
class HelloController {
@GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
}
另外,将
System.out.println
更改为更像Kotlin的println
。我认为您的问题在于您有一个没有主体的顶级类(HelloController
)和一个顶级函数(hello
)。您必须使用大括号来确保hello
是HelloController
的成员
你有这个:
@Controller
class HelloController
@GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
它需要是这样的,因此hello
属于HelloController
,而不是在同一级别:
@Controller
class HelloController {
@GetMapping("/hello")
fun hello(): String {
System.out.println("Hello from controller")
return "hello"
}
}
另外,将
System.out.println
更改为更像Kotlin的println
。使用@RestController
而不是@Controller
。当前控制器将尝试定位名为hello
的视图。请使用@RestController
而不是@controller
。您当前的控制器将尝试定位名为hello
的视图。您说得对!非常感谢你。现在它工作正常了。你说得对!非常感谢你。现在它工作正常了。