@Inject not working on Quarkus + Vertx + Kotlin

I am trying to write a simple reactive application using Quarkus and Kotlin with just one endpoint and using Vertx. But this simple piece of code does not work:

package com.acme

import io.quarkus.vertx.web.Route
import io.vertx.core.Vertx
import io.vertx.core.http.HttpMethod
import io.vertx.ext.web.RoutingContext
import javax.enterprise.context.ApplicationScoped
import javax.inject.Inject

@ApplicationScoped
open class Routing {

    @Inject
    lateinit var vertx: Vertx

    @Route(path = "/hello", methods = [HttpMethod.GET])
    fun handle(rc: RoutingContext) {
        println(vertx)
        rc.response().end("hello")
    }
}

I get this exception:

kotlin.UninitializedPropertyAccessException: lateinit property vertx has not been initialized
        at com.acme.Routing.handle(Routing.kt:18)

I have read Quarkus documentation about Kotlin (it's still a preview technology) and says something about using '@field: xxx' but I have tried a lot of things and none worked. It would be greatly appreciated if someone knows the answer.

I have tried the same in Java and works perfectly. @Inject Vertx or @Inject EventBus but in Kotlin seems impossible

Upvotes: 0

Views: 1355

Answers (2)

dankito
dankito

Reputation: 1108

According to https://quarkus.io/guides/kotlin#cdi-inject-with-kotlin one should add @field: Default "to handle the lack of a @Target on the Kotlin reflection annotation definition":

    @Inject
    @field: Default
    lateinit var vertx: Vertx

Upvotes: 1

dankito
dankito

Reputation: 1108

I took your example class and it's working fine: https://github.com/dankito/QuarkusKotlinInjetStackoverflowQuestion.

(Simply execute ./gradlew quarkusDev in project folder.)

Did you setup your project with the Quarkus project creator https://code.quarkus.io/?

Check "Eclipse Vert.x", "Eclipse Vert.x - Web" and "Kotlin" (add the bottom) there.

You then should have the following dependencies on your class path (Gradle notation): - org.jetbrains.kotlin:kotlin-stdlib-jdk8 - io.quarkus:quarkus-kotlin - io.quarkus:quarkus-vertx - io.quarkus:quarkus-vertx-web

Upvotes: 1

Related Questions