Skip to content

Commit d51b1b4

Browse files
committed
BAEL-21 Java 9 HttpClient code refactoring
1 parent 3daa1b0 commit d51b1b4

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

core-java-9/compile-httpclient.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
javac --module-path mods -d mods/com.baeldung.httpclient^
2+
src/modules/com.baeldung.httpclient/module-info.java^
3+
src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java

core-java-9/src/main/java/module-info.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

core-java-9/src/main/java/com/baeldung/java9/httpclient/HttpClientExample.java renamed to core-java-9/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* To change this template file, choose Tools | Templates
44
* and open the template in the editor.
55
*/
6-
package com.baeldung.java9.httpclient;
6+
package com.baeldung.httpclient;
77

88
import java.io.File;
99
import java.io.IOException;
@@ -25,8 +25,15 @@
2525
* @author pkaria
2626
*/
2727
public class HttpClientExample {
28+
29+
public static void main(String[] args) throws Exception {
30+
httpGetRequest();
31+
httpPosttRequest();
32+
asynchronousRequest();
33+
asynchronousMultipleRequests();
34+
}
2835

29-
public void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
36+
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
3037
HttpClient client = HttpClient.newHttpClient();
3138
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
3239
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
@@ -36,7 +43,7 @@ public void httpGetRequest() throws URISyntaxException, IOException, Interrupted
3643
System.out.println(responseBody);
3744
}
3845

39-
public void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
46+
public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
4047
HttpClient client = HttpClient
4148
.newBuilder()
4249
.build();
@@ -50,15 +57,15 @@ public void httpPosttRequest() throws URISyntaxException, IOException, Interrupt
5057
System.out.println(responseBody);
5158
}
5259

53-
public void asynchronousRequest() throws URISyntaxException {
60+
public static void asynchronousRequest() throws URISyntaxException {
5461
HttpClient client = HttpClient.newHttpClient();
5562
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
5663
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
5764
CompletableFuture<HttpResponse<String>> futureResponse = client.sendAsync(request,
5865
HttpResponse.BodyHandler.asString());
5966
}
6067

61-
public void asynchronousMultipleRequests() throws URISyntaxException {
68+
public static void asynchronousMultipleRequests() throws URISyntaxException {
6269
List<URI> targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2"));
6370
HttpClient client = HttpClient.newHttpClient();
6471
List<CompletableFuture<File>> futures = targets
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module com.baeldung.httpclient {
2+
requires jdk.incubator.httpclient;
3+
}

0 commit comments

Comments
 (0)