【问题标题】:Can Apache cxf rest client proxy work with reactive interfaceApache cxf rest客户端代理可以与反应接口一起使用吗
【发布时间】:2021-09-12 15:22:02
【问题描述】:

假设我有一个描述另一个休息服务的接口。它看起来像这样:

@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);
}

楼上的代码不起作用。这是我的看法。我的问题是有什么方法可以实现这样的功能

或者也许有人知道另一个可以构建这样的反应式休息代理的框架

【问题讨论】:

    标签: java rest jax-rs cxf feign


    【解决方案1】:

    我找到了解决方案。 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多