Kotlin的弹簧有问题。当我设置@RestController并转到localhost:8080时,我可以看到“索引”。如果设置@Controller,则有
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Wed May 13 23:34:59 EEST 2020
There was an unexpected error (type=Not Found, status=404).
我不知道为什么它不起作用。在Java中类似的代码(来自教程)工作。我的代码基于Java教程中的代码。我的App.kt
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
SomeController
@Controller
class SomeController {
@GetMapping("/")
fun index(model: Model): String {
model["something"] = "asd"
return "index"
}
}
index.mustache:
<html>
<body>
<div>
its the {{something}}
</div>
</body>
</html>
和pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
先谢谢您
使用Java,它可以正常工作-_-。相同的控制器和其他文件。
您没有在pom.xml中添加模板引擎,请添加此:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>