okhttpclient入口的假期

发布于 2025-01-21 06:47:28 字数 549 浏览 0 评论 0原文

使用OkhttpClient,我可以做以下操作,这将使我在可能的情况下发送请求并处理响应。

okhttpClient
    .newCall(request)
    .enqueue(
        new Callback() {

          @Override
          public void onResponse(Call arg0, Response arg1) throws IOException {
            log.info("Call success", arg1);
          }

          @Override
          public void onFailure(Call arg0, IOException arg1) {
            log.info("Call fail", arg1);
          }
        });

有没有办法用假装来做到这一点?如果有帮助,我已经启用了okhttpclient进行假装吗?

Using OkHttpClient, I can do the following, which would allow me to send the request and process the response when possible.

okhttpClient
    .newCall(request)
    .enqueue(
        new Callback() {

          @Override
          public void onResponse(Call arg0, Response arg1) throws IOException {
            log.info("Call success", arg1);
          }

          @Override
          public void onFailure(Call arg0, IOException arg1) {
            log.info("Call fail", arg1);
          }
        });

Is there a way to do this with Feign? I have enabled the okhttpclient for Feign, if that helps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2025-01-28 06:47:28

See https://github.com/OpenFeign/feign#async-execution-via -completableFuture

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  CompletableFuture<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

public class MyApp {
  public static void main(String... args) {
    GitHub github = AsyncFeign.asyncBuilder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");

由于其OKHTTP客户端不是异步,您要么需要使用

asyncclient.asyncclient(client,executorService),要么更优化地实现Okhttpasyncclient。

或rx

public interface GitHubReactiveX {
      
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
  
  class Contributor {
    String login;
    
    public Contributor(String login) {
      this.login = login;
    }
  }
}

public class ExampleRxJava2 {
  public static void main(String args[]) {
    GitHubReactiveX gitHub = RxJavaFeign.builder()      
      .target(GitHub.class, "https://api.github.com");
    
    List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
      .collect(Collectors.toList())
      .block();
  }
}

See https://github.com/OpenFeign/feign#async-execution-via-completablefuture

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  CompletableFuture<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

public class MyApp {
  public static void main(String... args) {
    GitHub github = AsyncFeign.asyncBuilder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");

Because their OkHttp client isn't async you would either need to use

AsyncClient.AsyncClient(client, executorService) or more optimally implement an OkHttpAsyncClient.

or RX https://github.com/OpenFeign/feign/tree/master/reactive

public interface GitHubReactiveX {
      
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
  
  class Contributor {
    String login;
    
    public Contributor(String login) {
      this.login = login;
    }
  }
}

public class ExampleRxJava2 {
  public static void main(String args[]) {
    GitHubReactiveX gitHub = RxJavaFeign.builder()      
      .target(GitHub.class, "https://api.github.com");
    
    List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
      .collect(Collectors.toList())
      .block();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文