Skip to content

Commit d615968

Browse files
committed
java 11
1 parent 60ee0f8 commit d615968

11 files changed

Lines changed: 406 additions & 0 deletions

File tree

java-11/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<artifactId>commons-codec</artifactId>
3535
<version>1.14</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.jetbrains</groupId>
39+
<artifactId>annotations</artifactId>
40+
<version>RELEASE</version>
41+
<scope>compile</scope>
42+
</dependency>
3743

3844
</dependencies>
3945

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.mkyong.java11;
2+
3+
public class HelloApp {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Hello Java 11");
8+
9+
}
10+
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.net.URI;
4+
import java.net.http.HttpClient;
5+
import java.net.http.HttpRequest;
6+
import java.net.http.HttpResponse;
7+
import java.time.Duration;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.TimeUnit;
10+
11+
public class HttpClientAsynchronous {
12+
13+
private static final HttpClient httpClient = HttpClient.newBuilder()
14+
.version(HttpClient.Version.HTTP_2)
15+
.connectTimeout(Duration.ofSeconds(10))
16+
.build();
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
HttpRequest request = HttpRequest.newBuilder()
21+
.GET()
22+
.uri(URI.create("https://httpbin.org/get"))
23+
.setHeader("User-Agent", "Java 11 HttpClient Bot")
24+
.build();
25+
26+
CompletableFuture<HttpResponse<String>> response =
27+
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
28+
29+
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
30+
31+
System.out.println(result);
32+
33+
}
34+
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.io.IOException;
4+
import java.net.Authenticator;
5+
import java.net.PasswordAuthentication;
6+
import java.net.URI;
7+
import java.net.http.HttpClient;
8+
import java.net.http.HttpRequest;
9+
import java.net.http.HttpResponse;
10+
import java.time.Duration;
11+
12+
// start this https://mkyong.com/spring-boot/spring-rest-spring-security-example/
13+
public class HttpClientAuthentication {
14+
15+
private static final HttpClient httpClient = HttpClient.newBuilder()
16+
.authenticator(new Authenticator() {
17+
@Override
18+
protected PasswordAuthentication getPasswordAuthentication() {
19+
return new PasswordAuthentication(
20+
"user",
21+
"password".toCharArray());
22+
}
23+
24+
})
25+
.connectTimeout(Duration.ofSeconds(10))
26+
.build();
27+
28+
public static void main(String[] args) throws IOException, InterruptedException {
29+
30+
HttpRequest request = HttpRequest.newBuilder()
31+
.GET()
32+
.uri(URI.create("http://localhost:8080/books"))
33+
.setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header
34+
.build();
35+
36+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
37+
38+
// print status code
39+
System.out.println(response.statusCode());
40+
41+
// print response body
42+
System.out.println(response.body());
43+
44+
}
45+
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.net.URI;
4+
import java.net.http.HttpClient;
5+
import java.net.http.HttpRequest;
6+
import java.net.http.HttpResponse;
7+
import java.time.Duration;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.stream.Collectors;
14+
15+
public class HttpClientCustomExecutor {
16+
17+
// custom executor
18+
private static final ExecutorService executorService = Executors.newFixedThreadPool(5);
19+
20+
private static final HttpClient httpClient = HttpClient.newBuilder()
21+
.executor(executorService)
22+
.version(HttpClient.Version.HTTP_2)
23+
.connectTimeout(Duration.ofSeconds(10))
24+
.build();
25+
26+
public static void main(String[] args) throws Exception {
27+
28+
List<URI> targets = Arrays.asList(
29+
new URI("https://httpbin.org/get?name=mkyong1"),
30+
new URI("https://httpbin.org/get?name=mkyong2"),
31+
new URI("https://httpbin.org/get?name=mkyong3"));
32+
33+
List<CompletableFuture<String>> result = targets.stream()
34+
.map(url -> httpClient.sendAsync(
35+
HttpRequest.newBuilder(url)
36+
.GET()
37+
.setHeader("User-Agent", "Java 11 HttpClient Bot")
38+
.build(),
39+
HttpResponse.BodyHandlers.ofString())
40+
.thenApply(response -> response.body()))
41+
.collect(Collectors.toList());
42+
43+
for (CompletableFuture<String> future : result) {
44+
System.out.println(future.get());
45+
}
46+
47+
}
48+
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URLEncoder;
6+
import java.net.http.HttpClient;
7+
import java.net.http.HttpRequest;
8+
import java.net.http.HttpResponse;
9+
import java.nio.charset.StandardCharsets;
10+
import java.time.Duration;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
public class HttpClientPostForm {
15+
16+
private static final HttpClient httpClient = HttpClient.newBuilder()
17+
.version(HttpClient.Version.HTTP_2)
18+
.connectTimeout(Duration.ofSeconds(10))
19+
.build();
20+
21+
public static void main(String[] args) throws IOException, InterruptedException {
22+
23+
// form parameters
24+
Map<Object, Object> data = new HashMap<>();
25+
data.put("username", "abc");
26+
data.put("password", "123");
27+
data.put("custom", "secret");
28+
data.put("ts", System.currentTimeMillis());
29+
30+
HttpRequest request = HttpRequest.newBuilder()
31+
.POST(ofFormData(data))
32+
.uri(URI.create("https://httpbin.org/post"))
33+
.setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header
34+
.header("Content-Type", "application/x-www-form-urlencoded")
35+
.build();
36+
37+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
38+
39+
// print status code
40+
System.out.println(response.statusCode());
41+
42+
// print response body
43+
System.out.println(response.body());
44+
45+
}
46+
47+
// Sample: 'password=123&custom=secret&username=abc&ts=1570704369823'
48+
public static HttpRequest.BodyPublisher ofFormData(Map<Object, Object> data) {
49+
var builder = new StringBuilder();
50+
for (Map.Entry<Object, Object> entry : data.entrySet()) {
51+
if (builder.length() > 0) {
52+
builder.append("&");
53+
}
54+
builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
55+
builder.append("=");
56+
builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
57+
}
58+
return HttpRequest.BodyPublishers.ofString(builder.toString());
59+
}
60+
61+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
import java.time.Duration;
9+
10+
public class HttpClientPostJSON {
11+
12+
private static final HttpClient httpClient = HttpClient.newBuilder()
13+
.version(HttpClient.Version.HTTP_2)
14+
.connectTimeout(Duration.ofSeconds(10))
15+
.build();
16+
17+
public static void main(String[] args) throws IOException, InterruptedException {
18+
19+
// json formatted data
20+
String json = new StringBuilder()
21+
.append("{")
22+
.append("\"name\":\"mkyong\",")
23+
.append("\"notes\":\"hello\"")
24+
.append("}").toString();
25+
26+
// add json header
27+
HttpRequest request = HttpRequest.newBuilder()
28+
.POST(HttpRequest.BodyPublishers.ofString(json))
29+
.uri(URI.create("https://httpbin.org/post"))
30+
.setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header
31+
.header("Content-Type", "application/json")
32+
.build();
33+
34+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
35+
36+
// print status code
37+
System.out.println(response.statusCode());
38+
39+
// print response body
40+
System.out.println(response.body());
41+
42+
}
43+
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.mkyong.java11.jep321;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpHeaders;
7+
import java.net.http.HttpRequest;
8+
import java.net.http.HttpResponse;
9+
import java.time.Duration;
10+
11+
public class HttpClientSynchronous {
12+
13+
/*
14+
HttpClient httpClient = HttpClient.newBuilder()
15+
.version(HttpClient.Version.HTTP_2)
16+
.followRedirects(HttpClient.Redirect.NORMAL)
17+
.connectTimeout(Duration.ofSeconds(20))
18+
.proxy(ProxySelector.of(new InetSocketAddress("proxy.yourcompany.com", 80)))
19+
.authenticator(Authenticator.getDefault())
20+
.build();
21+
*/
22+
23+
private static final HttpClient httpClient = HttpClient.newBuilder()
24+
.version(HttpClient.Version.HTTP_1_1)
25+
.connectTimeout(Duration.ofSeconds(10))
26+
.build();
27+
28+
public static void main(String[] args) throws IOException, InterruptedException {
29+
30+
HttpRequest request = HttpRequest.newBuilder()
31+
.GET()
32+
.uri(URI.create("https://httpbin.org/get"))
33+
.setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header
34+
.build();
35+
36+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
37+
38+
// print response headers
39+
HttpHeaders headers = response.headers();
40+
headers.map().forEach((k, v) -> System.out.println(k + ":" + v));
41+
42+
// print status code
43+
System.out.println(response.statusCode());
44+
45+
// print response body
46+
System.out.println(response.body());
47+
48+
}
49+
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mkyong.java11.jep323;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class LocalVar {
10+
11+
public static void main(String[] args) {
12+
13+
List<String> list = Arrays.asList("a", "b", "c");
14+
String result = list.stream()
15+
.map(x -> x.toUpperCase())
16+
.collect(Collectors.joining(","));
17+
System.out.println(result);
18+
19+
String result2 = list.stream()
20+
.map((var x) -> x.toUpperCase())
21+
.collect(Collectors.joining(","));
22+
System.out.println(result2);
23+
24+
List<String> list2 = Arrays.asList("a", "b", "c", null);
25+
String result3 = list2.stream()
26+
.map((@NotNull var x) -> x.toUpperCase())
27+
.collect(Collectors.joining(","));
28+
System.out.println(result3);
29+
30+
31+
}
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/opt/java/openjdk/bin/java --source 11
2+
public class SheBang {
3+
4+
public static void main(String[] args) {
5+
6+
System.out.println("Hello World!");
7+
8+
}
9+
}

0 commit comments

Comments
 (0)