Java 9 Example

This commit is contained in:
Umesh Awasthi
2018-04-29 09:30:54 -07:00
parent 545a7028d6
commit f362af9122
6 changed files with 123 additions and 0 deletions

View File

@@ -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<String> response= client.send(request, HttpResponse.BodyHandler.asString());
System.out.println(response.statusCode());
if(response.statusCode() == 200){
System.out.println(response.version());
//process business logic
}
}
}

View File

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

53
java-9/pom.xml Normal file
View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevjournal</groupId>
<artifactId>java-9</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-9</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,7 @@
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}

View File

@@ -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());
}
}

View File

@@ -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"));
}
}