Java 9 Example
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module com.javadevjournal.httpclient {
|
||||
requires jdk.incubator.httpclient;
|
||||
}
|
||||
53
java-9/pom.xml
Normal file
53
java-9/pom.xml
Normal 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>
|
||||
7
java-9/src/main/java/hello/Greeter.java
Executable file
7
java-9/src/main/java/hello/Greeter.java
Executable file
@@ -0,0 +1,7 @@
|
||||
package hello;
|
||||
|
||||
public class Greeter {
|
||||
public String sayHello() {
|
||||
return "Hello world!";
|
||||
}
|
||||
}
|
||||
12
java-9/src/main/java/hello/HelloWorld.java
Executable file
12
java-9/src/main/java/hello/HelloWorld.java
Executable 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());
|
||||
}
|
||||
}
|
||||
17
java-9/src/test/java/hello/GreeterTest.java
Executable file
17
java-9/src/test/java/hello/GreeterTest.java
Executable 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"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user