BAEL-21 Java 9 HttpClient code refactoring

This commit is contained in:
parthkaria 2017-12-01 10:51:33 +05:30
parent 3daa1b01ce
commit d51b1b4f20
4 changed files with 18 additions and 8 deletions

View File

@ -0,0 +1,3 @@
javac --module-path mods -d mods/com.baeldung.httpclient^
src/modules/com.baeldung.httpclient/module-info.java^
src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java

View File

@ -1,3 +0,0 @@
module com.baeldung.java9.httpclient {
requires jdk.incubator.httpclient;
}

View File

@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package com.baeldung.java9.httpclient; package com.baeldung.httpclient;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -26,7 +26,14 @@ import jdk.incubator.http.HttpResponse.BodyHandler;
*/ */
public class HttpClientExample { public class HttpClientExample {
public void httpGetRequest() throws URISyntaxException, IOException, InterruptedException { public static void main(String[] args) throws Exception {
httpGetRequest();
httpPosttRequest();
asynchronousRequest();
asynchronousMultipleRequests();
}
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
@ -36,7 +43,7 @@ public class HttpClientExample {
System.out.println(responseBody); System.out.println(responseBody);
} }
public void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException { public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient HttpClient client = HttpClient
.newBuilder() .newBuilder()
.build(); .build();
@ -50,7 +57,7 @@ public class HttpClientExample {
System.out.println(responseBody); System.out.println(responseBody);
} }
public void asynchronousRequest() throws URISyntaxException { public static void asynchronousRequest() throws URISyntaxException {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
@ -58,7 +65,7 @@ public class HttpClientExample {
HttpResponse.BodyHandler.asString()); HttpResponse.BodyHandler.asString());
} }
public void asynchronousMultipleRequests() throws URISyntaxException { public static void asynchronousMultipleRequests() throws URISyntaxException {
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2")); List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
List<CompletableFuture<File>> futures = targets List<CompletableFuture<File>> futures = targets

View File

@ -0,0 +1,3 @@
module com.baeldung.httpclient {
requires jdk.incubator.httpclient;
}