HttpClient was officially introduced with Java 11. It was first introduced in Java 9 as an incubator. You could say it is a new version of java.net.HttpUrlConnection.
It offers many new features:
- Supports both HTTP 1.1 and HTTP 2 (default)
- Supports both synchronous and asynchronous calls
- Provides reactive data and streams to both request and response with non-blocking back pressure
It works very well in asynchronous mode and with streams. However, here, we'll only cover the synchronous calls to align with other REST clients.
First, we'll add a provider class that will build the HttpClient and provide methods to build and send HTTP requests, as shown in the following example:
public class RestClient {
HttpClient httpClient = HttpClient
.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL...