我试图为我的post-service添加一个quarkus-rest-client
示例,这是一个用Quarkus构建的简单REST API。
Java版本运行良好。
当我添加另一个Kotlin来测试Quarkus中的kotlin和Gradle支持时,它失败了,不能将REST Client接口作为CDI bean注入。
PostControlloer
是Jaxrs资源,用于公开结合了原始两个API的聚合API。
@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()
}
}
以上两种注入Bean的方法均失败。
REST客户端界面:
@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
}
此Rest Client界面的应用程序配置。
com.example.PostResourceclient/mp-rest/url=http://localhost:8080
com.example.PostResourceclient/mp-rest/scope=javax.inject.Singleton
完整的代码是here。