Delving into the java.net.http Module
Java 11 adds yet another way to use HTTP from your Java code. In the new java.net.http
module, you'll find a class named HttpClient
. The HttpClient
class uses a modern builder pattern
(also called a fluent API) to set up HTTP connections. It then uses a Reactive Streams model to support both synchronous and asynchronous requests.
Note
You can refer to Chapter 16, Predicates and Other Functional Interfaces, and Chapter 17, Reactive Programming with Java Flow, for more on Java's Stream API and Reactive Streams. See https://packt.live/32sdPfO for an overview of the java.net.http
package in the module.
With the builder pattern, you do not instantiate a class with the new
operator. Instead, you start a builder, call methods to customize the resulting object, and then call the build()
method.
To start a builder with the HttpClient
class, call newBuilder()
. This begins the process of creating a customized HttpClient
object. Next...