Apache CXF的REST客户端代理本身不支持反应式接口,但可以使用 reactive-jaxrs-client 库与反应式接口一起使用。该库提供了与 Reactor 和 RxJava 2 兼容的方法来调用 REST 服务。
以下是使用 reactive-jaxrs-client 库的示例代码:
@Produces(MediaType.APPLICATION_JSON) @Path("/api/") interface MyService {
@GET
@Path("users/{userId}")
fun getUser(@PathParam("userId") userId: String): DeferredResult
}
val retrofit = CXFRetrofit.create( serviceInterface = MyService::class.java, baseUrl = "http://localhost:8080" )
val myService = retrofit.create(MyService::class.java)
val result = myService.getUser("123")
result.toMono() .subscribe { user -> println("User: $user") }