Java 9 Dependency Injection
上QQ阅读APP看书,第一时间看更新

HTTP/2.0 support

We used to connect servers using HttpURLConnection, which works in a single request/response cycle, and this eventually increases web page loading time and latency.

Moreover, the difference between HTTP/1.1 of older JDK and HTTP/2 of JAVA 9 is that data is framed when transporting between clients and servers. HTTP/2 uses the HttpClient API to push data by using the server push feature, with this it allows us to prioritize and send required data for loading the web page first. The following example shows HTTP interaction for the GET method:

//Get the HttpClient object
HttpClient client = HttpClient.newHttpClient();

// GET Method call
HttpResponse<String> getResponse = client.send(
HttpRequest
.newBuilder(new URI("http://www.xyz.com/")
.GET()
.build(),
BodyHandler.asString()
);
//Response of call
int responseStatus = getResponse.statusCode();
String body = responseStatus.body();