2

I tried to add a quarkus-rest-client sample for my post-service which is a simple REST API built with Quarkus.

The java version is working well.

When I added another Kotlin to test the kotlin and Gradle support in Quarkus, it failed, the REST Client interface can not be injected as CDI bean.

The PostControlloer is Jaxrs resource to expose an aggregated APIs that combined the original two APIs.

@Path("/api")
@RequestScoped
class PostController(@Inject @RestClient val client: PostResourceClient) {

//    @Inject
//    @RestClient
//    lateinit var client: PostServiceClient

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    fun getPosts(@QueryParam("q")
                 q: String,
                 @QueryParam("offset")
                 @DefaultValue("0")
                 offset: Int,
                 @QueryParam("limit")
                 @DefaultValue("10")
                 limit: Int): Response {
        val posts = this.client.getAllPosts(q, offset, limit).entity as List<Post>
        val count = this.client.countAllPosts(q).entity as Long
        return ok(PostPage(posts, count)).build()
    }

}

The above two approaches to inject a Bean are failed.

The REST Client interface:

@Path("/posts")
@RegisterRestClient
interface PostResourceClient {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    fun getAllPosts(
            @QueryParam("q")
            q: String,
            @QueryParam("offset")
            @DefaultValue("0")
            offset: Int,
            @QueryParam("limit")
            @DefaultValue("10")
            limit: Int
    ): Response

    @GET
    @Path("count")
    @Produces(MediaType.APPLICATION_JSON)
    fun countAllPosts(
            @QueryParam("q")
            q: String
    ): Response

}

The application config for this Rest Client interface.

com.example.PostResourceClient/mp-rest/url=http://localhost:8080
com.example.PostResourceClient/mp-rest/scope=javax.inject.Singleton

The complete codes is here.

Hantsy
  • 8,006
  • 7
  • 64
  • 109

1 Answers1

4

Duplicated with Error to inject some dependency with kotlin + quarkus it is a MicroProfile RestClient issue. See the workaround in the original SO answer:

@Inject
@field: RestClient
lateinit internal var countriesService: CountriesService

An issue is already openned on MicroProfile RestClient to have a fix for this and tracked on the Quarkus issue traker: https://github.com/quarkusio/quarkus/issues/5413

Tim Brückner
  • 1,928
  • 2
  • 16
  • 27
loicmathieu
  • 5,181
  • 26
  • 31