From f362af912228092710550f05b9fd881240d1e4aa Mon Sep 17 00:00:00 2001 From: Umesh Awasthi Date: Sun, 29 Apr 2018 09:30:54 -0700 Subject: [PATCH] Java 9 Example --- .../javadevjournal/httpclient/HTTPClient.java | 31 +++++++++++ .../src/module-info.java | 3 ++ java-9/pom.xml | 53 +++++++++++++++++++ java-9/src/main/java/hello/Greeter.java | 7 +++ java-9/src/main/java/hello/HelloWorld.java | 12 +++++ java-9/src/test/java/hello/GreeterTest.java | 17 ++++++ 6 files changed, 123 insertions(+) create mode 100644 java-9/com.javadevjournal.httpclient/src/com/javadevjournal/httpclient/HTTPClient.java create mode 100644 java-9/com.javadevjournal.httpclient/src/module-info.java create mode 100644 java-9/pom.xml create mode 100755 java-9/src/main/java/hello/Greeter.java create mode 100755 java-9/src/main/java/hello/HelloWorld.java create mode 100755 java-9/src/test/java/hello/GreeterTest.java diff --git a/java-9/com.javadevjournal.httpclient/src/com/javadevjournal/httpclient/HTTPClient.java b/java-9/com.javadevjournal.httpclient/src/com/javadevjournal/httpclient/HTTPClient.java new file mode 100644 index 0000000..b20adcb --- /dev/null +++ b/java-9/com.javadevjournal.httpclient/src/com/javadevjournal/httpclient/HTTPClient.java @@ -0,0 +1,31 @@ +package com.javadevjournal.httpclient; + +import jdk.incubator.http.HttpClient; +import jdk.incubator.http.HttpHeaders; +import jdk.incubator.http.HttpRequest; +import jdk.incubator.http.HttpResponse; + +import java.io.IOException; +import java.net.*; +import java.util.concurrent.CompletableFuture; + +import static jdk.incubator.http.HttpResponse.BodyHandler.asString; + +public class HTTPClient { + + public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { + + byte[] requestBody ="This is expected to be sent back as part of response body".getBytes(); + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://postman-echo.com/post")).POST(HttpRequest.BodyProcessor.fromString("This is expected to be sent back as part of response body")) + .build(); + + HttpResponse response= client.send(request, HttpResponse.BodyHandler.asString()); + System.out.println(response.statusCode()); + if(response.statusCode() == 200){ + System.out.println(response.version()); + //process business logic + } + } +} diff --git a/java-9/com.javadevjournal.httpclient/src/module-info.java b/java-9/com.javadevjournal.httpclient/src/module-info.java new file mode 100644 index 0000000..fb492cb --- /dev/null +++ b/java-9/com.javadevjournal.httpclient/src/module-info.java @@ -0,0 +1,3 @@ +module com.javadevjournal.httpclient { + requires jdk.incubator.httpclient; +} \ No newline at end of file diff --git a/java-9/pom.xml b/java-9/pom.xml new file mode 100644 index 0000000..54e2bc0 --- /dev/null +++ b/java-9/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.javadevjournal + java-9 + 1.0-SNAPSHOT + + + 1.9 + 1.9 + UTF-8 + 4.12 + 3.7.0 + 2.19.1 + + + + + junit + junit + ${junit.version} + test + + + + + core-java-9 + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + \ No newline at end of file diff --git a/java-9/src/main/java/hello/Greeter.java b/java-9/src/main/java/hello/Greeter.java new file mode 100755 index 0000000..74eb8d4 --- /dev/null +++ b/java-9/src/main/java/hello/Greeter.java @@ -0,0 +1,7 @@ +package hello; + +public class Greeter { + public String sayHello() { + return "Hello world!"; + } +} diff --git a/java-9/src/main/java/hello/HelloWorld.java b/java-9/src/main/java/hello/HelloWorld.java new file mode 100755 index 0000000..a85f595 --- /dev/null +++ b/java-9/src/main/java/hello/HelloWorld.java @@ -0,0 +1,12 @@ +package hello; + +import org.joda.time.LocalTime; + +public class HelloWorld { + public static void main(String[] args) { + LocalTime currentTime = new LocalTime(); + System.out.println("The current local time is: " + currentTime); + Greeter greeter = new Greeter(); + System.out.println(greeter.sayHello()); + } +} diff --git a/java-9/src/test/java/hello/GreeterTest.java b/java-9/src/test/java/hello/GreeterTest.java new file mode 100755 index 0000000..ef37fbe --- /dev/null +++ b/java-9/src/test/java/hello/GreeterTest.java @@ -0,0 +1,17 @@ +package hello; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.*; + +import org.junit.Test; + +public class GreeterTest { + + private Greeter greeter = new Greeter(); + + @Test + public void greeterSaysHello() { + assertThat(greeter.sayHello(), containsString("Hello")); + } + +}