java Jersey REST 客户端与 Apache HTTP 客户端 4.5 对比改造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42199614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:26:21  来源:igfitidea点击:

Jersey REST client with Apache HTTP Client 4.5 vs retrofit

javarestretrofitapache-httpclient-4.xjersey-client

提问by Chris Sim

I was reading many articles to find the best Rest Client for java application, I found finally using Jersey with Apache HTTP client 4.5 is great but in a lot of articles I found that now Retrofit is the best (I didn't mention Volley because in my case I don't need that the API supports caching.

我阅读了很多文章以找到适用于 Java 应用程序的最佳 Rest Client,我发现最终将 Jersey 与 Apache HTTP 客户端 4.5 一起使用很棒,但在很多文章中我发现现在 Retrofit 是最好的(我没有提到 Volley,因为在我的情况我不需要 API 支持缓存。

Does Retrofit is better for a java client application. or is it just better for android? and why I didn't find this comparison before .. they cannot be compared?

Retrofit 是否更适合 Java 客户端应用程序。或者它只是对android更好?为什么我之前没有找到这个比较..它们不能比较?

Can I have a comparison between their performance, connection pooling, on which layer do they work, compression of the requests and responses, Timeout, de-serialization?

我能否比较它们的性能、连接池、它们在哪一层工作、请求和响应的压缩、超时、反序列化?

HTTP3 does not support connection pooling, is that why retrofit is used usually for android ?? so It will not be practical for a normal java application where it will cause connection leak.

HTTP3 不支持连接池,这就是为什么改造通常用于 android 的原因??所以对于普通的java应用程序来说它会导致连接泄漏是不切实际的。

My target is to find the best Rest API client with a high performance, and support high number of connections.

我的目标是找到最好的具有高性能和支持大量连接的 Rest API 客户端。

Thank you in advance

先感谢您

回答by aha

You're mixing different things together. To clear things up up-front:

你把不同的东西混合在一起。预先清除事情:

Retrofitis a client library to interact with REST APIs. As such it offers the same abstraction level as Jersey, RESTeasyor Spring's RestTemplate. They all allow to interact with REST APIs using a type-safe API without having to deal with low level aspects like serialization, request building and response handling.

Retrofit是一个与 REST API 交互的客户端库。因此,它提供与JerseyRESTeasy或 Spring 的RestTemplate相同的抽象级别。它们都允许使用类型安全的 API 与 REST API 进行交互,而无需处理诸如序列化、请求构建和响应处理之类的低级方面。

Each of those libraries uses a HTTP client underneath to actually talk to an HTTP server. Examples are Apache HTTP clientthat you mentioned, OkHttpor the plain-old HttpUrlConnectionshipping with the JDK.

这些库中的每一个都使用下面的 HTTP 客户端来实际与 HTTP 服务器通信。示例是您提到的Apache HTTP 客户端OkHttp或与 JDK 一起提供的普通HttpUrlConnection

You can usually mix and match the different REST client libraries and HTTP clients except for Retrofit because Retrofit has a hard dependency on OkHttp since version 2(with Retrofit 1.x you can use Apache HTTP Client, HttpUrlConnection or OkHttp).

除了 Retrofit,您通常可以混合搭配不同的 REST 客户端库和 HTTP 客户端,因为 Retrofit 从版本 2 开始就对 OkHttp 有严格的依赖(对于 Retrofit 1.x,您可以使用 Apache HTTP Client、HttpUrlConnection 或 OkHttp)。

Back to the actual question: What to pick when.

回到实际问题:什么时候选什么。

Android: It's easy here because JAX-RS, the API/technology behind Jersey and RESTeasy isn't supported on Android. Hence Retrofit is more or less your only option except maybe Volley if you don't want to talk HTTP directly. Spring isn't available either and Spring Android is abandoned.

Android:这里很容易,因为JAX-RS、Jersey 和 RESTeasy 背后的 API/技术在 Android 上不受支持。因此,Retrofit 或多或少是您唯一的选择,如果您不想直接与 HTTP 通信,可能除了 Volley。Spring 也不可用,并且Spring Android 已被放弃

JRE/JDK: Here you have the full choice of options.

JRE/JDK:在这里您可以选择完整的选项。

  • Retrofit might be nice if you want a quick and easy solution to implement a third-party API for which no SDK is available or JAX-RS interfaces.
  • Spring's RestTemplate is a good choice if you're using Spring and there are no JAX-RS interfaces or you don't want to buy into JAX-RS, i.e. also using it on the server-side.
  • JAX-RS (Jersey, RESTeasy, …) is a good choice if you want to share interface definitions between client and servers or if you're all-in on JavaEE anyway.
  • 如果您想要一个快速简单的解决方案来实现没有可用的 SDK 或 JAX-RS 接口的第三方 API,改造可能会很好。
  • 如果您使用的是 Spring 并且没有 JAX-RS 接口或者您不想购买 JAX-RS,即也在服务器端使用它,则 Spring 的 RestTemplate 是一个不错的选择。
  • 如果您想在客户端和服务器之间共享接口定义,或者您完全使用 JavaEE,JAX-RS(Jersey、RESTeasy 等)是一个不错的选择。

Regarding performance: The main drivers here is the time spent on doing HTTP and (de)serialization. Because (de)serialization is performed by specialized libraries like Hymanson or protobuf and all use the same (or you could at least make them to) there shouldn't be any meaningful difference.

关于性能:这里的主要驱动因素是执行 HTTP 和(反)序列化所花费的时间。因为(反)序列化是由像 Hymanson 或 protobuf 这样的专业库执行的,并且都使用相同的(或者您至少可以使它们如此),所以不应该有任何有意义的差异。

回答by Chris Sim

It took a while to find, however I have found the perfect REST client library that makes our development declarative and easy. We can use this as the standard when developing new REST implementations or APIs.

花了一段时间才找到,但是我找到了完美的 REST 客户端库,它使我们的开发声明式且简单。在开发新的 REST 实现或 API 时,我们可以将其用作标准。

It is called Feign, developed by the Netflix team and made to work with Spring Cloud Netflix. More details hereon the project's site.

它被称为 Feign,由 Netflix 团队开发并与 Spring Cloud Netflix 合作。更多细节在这里对项目的网站。

Some features include: - Integration with Hymanson, Gson and other Encoders/Decoders - Using OkHttp for network communication, a proven HTTP library - Binding with SLF4J for logging features - Interface-based implementation, minimal development. Below is a sample client:

一些功能包括: - 与 Hymanson、Gson 和其他编码器/解码器的集成 - 使用 OkHttp 进行网络通信,一个经过验证的 HTTP 库 - 与 SLF4J 绑定用于日志记录功能 - 基于接口的实现,最少的开发。下面是一个示例客户端:

@FeignClient("stores")
public interface StoreClient
{
   @RequestMapping(method = RequestMethod.GET, value = "/stores")
   List<Store> getStores();

   @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
   Store update(@PathVariable("storeId") Long storeId, Store store);
}

And after @aha 's answer as quoted below:

在@aha 的回答之后,引用如下:

JRE/JDK: Here you have the full choice of options.

Retrofit might be nice if you want a quick and easy solution to implement a third-party API for which no SDK is available or JAX-RS interfaces.

Spring's RestTemplate is a good choice if you're using Spring and there are no JAX-RS interfaces or you don't want to buy into JAX-RS, i.e. also using it on the server-side.

JAX-RS (Jersey, RESTeasy, …) is a good choice if you want to share interface definitions between client and servers or if you're all-in on JavaEE anyway.

JRE/JDK:在这里您可以选择完整的选项。

如果您想要一个快速简单的解决方案来实现没有可用的 SDK 或 JAX-RS 接口的第三方 API,改造可能会很好。

如果您使用的是 Spring 并且没有 JAX-RS 接口或者您不想购买 JAX-RS,即也在服务器端使用它,则 Spring 的 RestTemplate 是一个不错的选择。

如果您想在客户端和服务器之间共享接口定义,或者您完全使用 JavaEE,JAX-RS(Jersey、RESTeasy 等)是一个不错的选择。

Feign works like retrofit and JAX-RS together: easy solution and can share interface definitions between client and servers and can use JAX-RS interfaces

Feign 像改造和 JAX-RS 一起工作:简单的解决方案,可以在客户端和服务器之间共享接口定义,可以使用 JAX-RS 接口