Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey REST client with Apache HTTP Client 4.5 vs retrofit

Tags:

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

like image 667
Chris Sim Avatar asked Feb 13 '17 08:02

Chris Sim


People also ask

Which HttpClient is best in Java?

Apache HttpClient The Apache HTTP client was the first widely adopted open source client to be released, arriving in its original form in 2002. Now on its 5th major version, it's probably still the most commonly used client outside of Java's core libraries.

What is the difference between Restclient and HttpClient?

HTTP client is a client that is able to send a request to and get a response from the server in HTTP format. REST client is a client that is designed to use a service from a server and this service is RESTful.

Does RestTemplate use HttpClient?

RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.

Is Apache HttpClient thread safe?

HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.


1 Answers

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.

like image 157
aha Avatar answered Oct 12 '22 10:10

aha