Spring在Kotlin中:Spring无法识别任何控制器

huangapple go评论72阅读模式
英文:

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(&quot;org.springframework.boot&quot;) version &quot;3.0.3&quot;
	id(&quot;io.spring.dependency-management&quot;) version &quot;1.1.0&quot;
	kotlin(&quot;jvm&quot;) version &quot;1.7.22&quot;
	kotlin(&quot;plugin.spring&quot;) version &quot;1.7.22&quot;
}

group = &quot;com.reacctivespring&quot;
version = &quot;0.0.1-SNAPSHOT&quot;
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
	mavenCentral()
}

dependencies {
	implementation(&quot;org.springframework.boot:spring-boot-starter-webflux&quot;)
	implementation(&quot;org.springframework.boot:spring-boot-starter-actuator&quot;)
	implementation(&quot;com.fasterxml.jackson.module:jackson-module-kotlin&quot;)
	implementation(&quot;io.projectreactor.kotlin:reactor-kotlin-extensions&quot;)
	implementation(&quot;org.jetbrains.kotlin:kotlin-reflect&quot;)
	implementation(&quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8&quot;)
	implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-reactor&quot;)
	testImplementation(&quot;org.springframework.boot:spring-boot-starter-test&quot;)
	testImplementation(&quot;io.projectreactor:reactor-test&quot;)
}

tasks.withType&lt;KotlinCompile&gt; {
	kotlinOptions {
		freeCompilerArgs = listOf(&quot;-Xjsr305=strict&quot;)
		jvmTarget = &quot;17&quot;
	}
}

tasks.withType&lt;Test&gt; {
	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&lt;String&gt;) {
	runApplication&lt;WebfluxApplication&gt;(*args)
}

@RestController
class ApiController() {
	@GetMapping(
		name = &quot;Getting my name&quot;,
		value = [&quot;/api/&quot;],
		produces = [MediaType.APPLICATION_JSON_VALUE]
	)
	fun getDemo(
	): Flux&lt;Money&gt; {
		return Flux.interval(Duration.ofSeconds(1))
			.map {
				Money(&quot;Testing&quot;)
			}
	}

}

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.

huangapple
  • 本文由 发表于 2023年2月27日 02:39:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定