Jersey REST client with Apache HTTP Client 4.5 vs retrofit

Chris Sim picture Chris Sim · Feb 13, 2017 · Viewed 13.7k times · Source

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

Answer

aha picture aha · Feb 21, 2017

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.