[英]Can Apache cxf rest client proxy work with reactive interface
假設我有一個描述另一個休息服務的接口。 它看起來像這樣:
@Produces("application/json")
public interface PetApi {
@GET
@Path("pet/{petId}")
String getPet(@PathParam("petId") Long petId);
}
當我調用它時它工作正常:
private static void testPet() {
PetApi petApi = JAXRSClientFactory.create("https://petstore.swagger.io/v2", PetApi.class);
String petDescr = petApi.getPet(1L);
System.out.println(petDescr);
}
現在我想改進這段代碼,以便在反應流中工作。 apache CXF 可以為響應式代碼生成工作代理嗎? 以這種方式調用代碼將如下所示:
private static void testPet() {
PetApi petApi = JAXRSClientFactory.create("https://petstore.swagger.io/v2", PetApi.class);
CompletionStage<String> petDescrCs = petApi.getPet(1L);
petDescrCs.thenAccept(System.out::println);
}
和外部休息服務的接口是:
@Produces("application/json")
public interface PetApi {
@GET
@Path("pet/{petId}")
CompletionStage<String> getPet(@PathParam("petId") Long petId);
}
樓上的代碼不行。 這是我的看法。 我的問題是有什么方法可以實現這樣的功能
或者也許有人知道另一個框架可以構建像這樣的反應式休息代理
我找到了解決方案。 https://github.com/OpenFeign/feign從 10.8 版本開始可以進行響應式調用。
見https://github.com/OpenFeign/feign#async-execution-via-completablefuture 。
應用於問題中的代碼...
依賴:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jaxrs</artifactId>
<version>11.2</version>
</dependency>
界面:
@Produces("application/json")
public interface PetApi {
// Reactive method. Work only with CompletableFuture(not work with CompletionStage)
@GET
@Path("pet/{petId}")
CompletableFuture<String> getPetCs(@PathParam("petId") Long petId);
// Non reactive method
@GET
@Path("pet/{petId}")
String getPet(@PathParam("petId") Long petId);
}
調用示例:
private static void reactiveTest() {
PetApi petApi = AsyncFeign.asyncBuilder()
.contract(new JAXRSContract()) // JAX-RS contract for fegin
.target(PetApi.class, "https://petstore.swagger.io/v2");
CompletionStage<String> petDescrCs = petApi.getPetCs(1L);
petDescrCs.thenAccept(System.out::println);
}
private static void noneReactiveTest() {
PetApi petApi = AsyncFeign.asyncBuilder()
.contract(new JAXRSContract()) // JAX-RS contract for fegin
.target(PetApi.class, "https://petstore.swagger.io/v2");
String petDescr = petApi.getPet(1L);
System.out.println(petDescr);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.