Search code examples
springkotlinspring-webfluxspring-restcontroller

Spring in kotlin: Spring doesn't see any controller


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?


Solution

  • 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.