BAEL-21 Java 9 HttpClient code refactoring
This commit is contained in:
parent
3daa1b01ce
commit
d51b1b4f20
|
@ -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
|
|
@ -1,3 +0,0 @@
|
||||||
module com.baeldung.java9.httpclient {
|
|
||||||
requires jdk.incubator.httpclient;
|
|
||||||
}
|
|
|
@ -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;
|
||||||
|
@ -25,8 +25,15 @@ import jdk.incubator.http.HttpResponse.BodyHandler;
|
||||||
* @author pkaria
|
* @author pkaria
|
||||||
*/
|
*/
|
||||||
public class HttpClientExample {
|
public class HttpClientExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
httpGetRequest();
|
||||||
|
httpPosttRequest();
|
||||||
|
asynchronousRequest();
|
||||||
|
asynchronousMultipleRequests();
|
||||||
|
}
|
||||||
|
|
||||||
public void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
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
|
|
@ -0,0 +1,3 @@
|
||||||
|
module com.baeldung.httpclient {
|
||||||
|
requires jdk.incubator.httpclient;
|
||||||
|
}
|
Loading…
Reference in New Issue