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.