25

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.

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?

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.

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

Thank you in advance

Chris Sim
  • 3,584
  • 3
  • 26
  • 32
  • Then don't just write "best" but "highest throughput" instead of listing a lot of partly conflicting criteria - maybe the best tool for deserialisation is worse in throughput, for instance. And provide sample code which you think is too slow or at least typical for your situation, so as to help others to optimise that for you. You don't get what StackOverflow is about, do you? Have you ever wondered why nobody wanted to answer the question before you put a bounty on it? Because nobody but you can really answer the question with unclear criteria - which is exactly what happened in the end. – kriegaex Feb 27 '17 at 14:07

2 Answers2

43

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

Retrofit is a client library to interact with REST APIs. As such it offers the same abstraction level as Jersey, RESTeasy or 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.

Each of those libraries uses a HTTP client underneath to actually talk to an HTTP server. Examples are Apache HTTP client that you mentioned, OkHttp or the plain-old HttpUrlConnection shipping with the JDK.

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).

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.

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.

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 Jackson or protobuf and all use the same (or you could at least make them to) there shouldn't be any meaningful difference.

aha
  • 3,326
  • 2
  • 32
  • 42
  • If I have a large number of requests at the same time, is retrofit2 a good choice, does it support stress test, do you have reference to benchmarks? – Chris Sim Feb 27 '17 at 07:25
  • Retrofit is a thin layer on top of OkHttp. Therefore looking at OkHttp directly might be more relevant. Benchmarks from the authors: https://github.com/square/okhttp/tree/master/benchmarks. Regarding stress tests: What do you have in mind? – aha Feb 27 '17 at 08:51
  • Thank you for your time to explain all of this. What do I have in my mind is that I wanted to use Retrofit2 based on okhttp3 but I was afraid if they don't support a huge number of requests at the same time. – Chris Sim Feb 27 '17 at 09:50
7

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.

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

Some features include: - Integration with Jackson, 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:

@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:

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.

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

Chris Sim
  • 3,584
  • 3
  • 26
  • 32
  • Feign occupies the Jersey/Jackson stack. But why do you write Feign apart from RestTemplate? AFAIK, Feign API clients interfaces would end using RestTemplate to start the call – Alex Jan 01 '21 at 21:54