Home > Software engineering >  Java Controller in Kotlin SpringBoot project
Java Controller in Kotlin SpringBoot project

Time:10-07

The new/empty project stub was generated using Spring Initializer on IntelliJ using Kotlin as the language choice along with Gradle.

However, adding a Java controller to the project root does not seem to be found when the application is run. All endpoints return a 404. I tested a Kotlin controller and it is working as expected.

Example Java Controller:

@Controller
public class TestController {
    @PostMapping("/")
    public String test() {
        return "test";
    }
}

Example Kotlin Controller:

@Controller
class TestController {
    @GetMapping("/")
    fun test(model: Model): String {
        return "test"
    }
}

build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.5.5"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.31"
    kotlin("plugin.spring") version "1.5.31"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

CodePudding user response:

I guess you are using Spring MVC. Are you sure that you have a view "test"? Because it can also be an issue. Try to annotate it with RestController instead of Controller and see if it's returning anything. For purpose of that test I would remove method parameter too

CodePudding user response:

It works fine for me. Make sure you put the java controller under source -> main -> java directory and not under source -> main -> kotlin Make sure java is enabled when you import the project in Intellij.

Here are the settings: DempApplication:

@SpringBootApplication
@ComponentScan("example")
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

Java Controller:

@RestController
public class UserController {
    @GetMapping("/user")
    public String test() {
        return "test";
    }
}

Dependencies:

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-data-rest")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compileOnly("org.projectlombok:lombok")
    runtimeOnly("org.hsqldb:hsqldb")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
  • Related