英文:
Spring in kotlin: Spring doesn't see any controller
问题
我从Spring Initializer创建了一个全新的应用程序。
我在其中只添加了WebFlux和Actuator... Gradle看起来像这样:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.3"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
}
group = "com.reacctivespring"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
我创建了一个Rest控制器,只是为了查看是Spring、我的设备还是代码中有错误。这是应用程序中的所有代码(是的,都在同一个文件中):
@SpringBootApplication
class WebfluxApplication
fun main(args: Array<String>) {
runApplication<WebfluxApplication>(*args)
}
@RestController
class ApiController() {
@GetMapping(
name = "Getting my name",
value = ["/api/"],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun getDemo(): Flux<Money> {
return Flux.interval(Duration.ofSeconds(1))
.map {
Money("Testing")
}
}
}
data class Money(val name: String)
我启用了所有的Actuator端点,并且我可以在浏览器中正确地打开它们。
但是当我访问API时,我得到404(未找到)的响应。
我尝试了这样的API地址:/api/
和api/
,但结果都一样。
我还尝试了Kotlin Flow,但结果也一样。
为什么会这样?我该如何修复它呢?
英文:
I created a fresh new application from spring initializer.
>https://start.spring.io/
and I added the WebFlux and Actuator in it only... the gradle looks like this
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.3"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
}
group = "com.reacctivespring"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I created a Rest controller just to see if there is an issue with spring or my device or an error in the code. this is all the code in the app (yes all in the same file)
@SpringBootApplication
class WebfluxApplication
fun main(args: Array<String>) {
runApplication<WebfluxApplication>(*args)
}
@RestController
class ApiController() {
@GetMapping(
name = "Getting my name",
value = ["/api/"],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun getDemo(
): Flux<Money> {
return Flux.interval(Duration.ofSeconds(1))
.map {
Money("Testing")
}
}
}
data class Money(val name: String)
i enabled all actuator endpoints and I can open them correctly in the browser.
but when I access the api, I get 404 (not found) response.
I tried the api address like this /api/
and this api/
, same same...
I tried kotlin flow also, but same same...
why is that?
how can i fix it?
答案1
得分: 2
根据我问题的评论,Spring Boot 的问题跟踪器建议从路径中移除所有空格。
> https://github.com/spring-projects/spring-boot/issues/34379
我已将所有文件夹中的空格移除,现在可以正常工作。
英文:
Based on the comment on my question.
the issue tracker for Spring Boot, we should remove all the spaces from the path.
>https://github.com/spring-projects/spring-boot/issues/34379
i removed the spaces from all the folders and it is working now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论