Jersey REST client with Apache HTTP Client 4.5 vs retrofit
Asked Answered
G

2

28

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

Godbeare answered 13/2, 2017 at 8:27 Comment(1)
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.Morass
A
48

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.

Atlas answered 21/2, 2017 at 19:49 Comment(3)
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?Godbeare
Retrofit is a thin layer on top of OkHttp. Therefore looking at OkHttp directly might be more relevant. Benchmarks from the authors: github.com/square/okhttp/tree/master/benchmarks. Regarding stress tests: What do you have in mind?Atlas
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.Godbeare
G
9

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

Godbeare answered 27/2, 2017 at 8:28 Comment(1)
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 callProffitt

© 2022 - 2024 — McMap. All rights reserved.