Introduction
REST APIs have been the center of integration for disparate systems. Java Ecosystem has seen different flavors of Rest Client( native + third party ) that allow consuming these RestFul APIs with abstractions.
Design for the Rest Client has been influenced by technology, best practices, and the need for developers to build RESTful web applications.
In this article, we will cover the evolution of Java Rest Client.
1.HttpURLConnection
In the early days of Java low-level libraries such as `java.net.HttpURLConnection` were mostly used.
The major problem with using HttpURLConnection is the lack of abstraction. Writing HTTP requests and handling responses required too much boilerplate code and manual.
2. Apache HttpClient
Apache HttpClient became a popular choice for writing rest clients in Java due to its abstraction for writing requests, managing connections, and handling responses.
3. JAX-RS/Jersey Client API
Java API for RESTFul web services ( JAX-RS ) introduced the `Client` API as part of the Java EE standard.
It offered a high-level and more declarative approach for building REST client. Developers could define client interfaces and use annotation to define request and response mappings.
Jersey is a popular JAX-RS implementation and includes its own client API.
It provided a convenient way to consume RESTFul APIs.
Sample Request Response Code:
4. Spring Rest Template
Spring Framework provided `RestTemplate` class as part of the web module.
Due to its simplified approach in writing and handling related tasks, it became the default choice in spring-based applications.
5. Spring WebClient
Due to the popularity of asynchronous programming and reactive programming, Spring introduced the `WebClient` class as part of the Spring WebFlux framework.
This non-blocking HTTP client allowed developers to make asynchronous and reactive REST calls.
6. Apache HttpClient 5
Apache HttpClient went under significant changes in version 5. HttpClient 5 introduced more modern and modular architecture and made it more suitable for modern Java applications.
It also embraced the changes introduced in Java 11 and later version.
7. OkHttp
OkHttp is a popular third-party HTTP client library used for Java and Android.
It's known for its popularity related to modern features such as connection pooling, and request and response interception.
Many developers preferred OkHttp to write and handle Http-related tasks.
8. Java 11+ HTTP Client
Starting Java 11, a new standard HTTP client library was introduced as part of
java.net.http
package.This native client is asynchronous and supports HTTP/2, WebSocket, and reactive programming.
It provided modern built-in features that obsolete the use of third-party libraries for handling HTTP-related tasks.
9. Feign
Feign is a declarative HTTP client developed by Netflix. It allows developers to define HTTP clients using annotation and interfaces similar to JAX-RS.
Feign has good integration with Spring and other frameworks.
10. Retrofit
Retrofit is a popular third-party library for creating REST clients in Java & Android. It's known for its simplicity and type-safe API. Developers define RESTFul API interfaces with annotation and Retrofit handles requests and response.
11. Others
Modern microservice frameworks such as micronaut and quarkus has come up with built-in support for creating efficient and lightweight REST Clients.
They have been designed for cloud-native application and ahead-of-time compilation with minimal resource consumption.
Conclusion
In this article, We have discussed various options and the evolution of Java Rest Client.
When writing simple Java-based apps, For most of the use cases Java HttpClient introduced in version 11 should be enough.
But if there specific framework that we are working on then using framework specific client make sense.