[[oktatas:programozás:java:java_rest_api_kliens|< Java REST API kliens]]
====== Java REST API kliens - HttpClient ======
* **Szerző:** Sallai András
* Copyright (c) 2024, Sallai András
* Szerkesztve: 2024
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== CRUD műveletek aszinkron módban =====
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.CompletableFuture;
public class ClientAsync {
public static void get() {
String url = "https://jsonplaceholder.typicode.com/users";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.GET();
CompletableFuture res =
client.sendAsync(request.build(),
BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(res.join());
}
public static void post() {
String url = "https://jsonplaceholder.typicode.com/users";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.header("Content-Type", "application/json");
String body = "{ \"name\": \"John\", \"age\": 30 }";
request.POST(HttpRequest.BodyPublishers.ofString(body));
CompletableFuture res =
client.sendAsync(request.build(),
BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(res.join());
}
public static void put() {
String url = "https://jsonplaceholder.typicode.com/users/1";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.header("Content-Type", "application/json");
String body = "{ \"name\": \"John\", \"age\": 30 }";
request.PUT(HttpRequest.BodyPublishers.ofString(body));
CompletableFuture res =
client.sendAsync(request.build(),
BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(res.join());
}
public static void delete() {
String url = "https://jsonplaceholder.typicode.com/users/1";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.DELETE();
CompletableFuture res =
client.sendAsync(request.build(),
BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(res.join());
}
}
===== CRUD műveletek szinkron módban =====
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse.BodyHandlers;
public class Client {
public static void get()
throws IOException, InterruptedException {
String url = "https://jsonplaceholder.typicode.com/users";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.GET();
HttpResponse res =
client.send(request.build(),
BodyHandlers.ofString());
System.out.println(res.body());
}
public static void post()
throws IOException, InterruptedException {
String url = "https://jsonplaceholder.typicode.com/users";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.header("Content-Type", "application/json");
String body = "{ \"name\": \"John\", \"age\": 30 }";
request.POST(HttpRequest.BodyPublishers.ofString(body));
HttpResponse res =
client.send(request.build(),
BodyHandlers.ofString());
System.out.println(res.body());
}
public static void put()
throws IOException, InterruptedException {
String url = "https://jsonplaceholder.typicode.com/users/1";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.header("Content-Type", "application/json");
String body = "{ \"name\": \"John\", \"age\": 30 }";
request.PUT(HttpRequest.BodyPublishers.ofString(body));
HttpResponse res =
client.send(request.build(),
BodyHandlers.ofString());
System.out.println(res.body());
}
public static void delete()
throws IOException, InterruptedException {
String url = "https://jsonplaceholder.typicode.com/users/1";
HttpClient client = HttpClient.newHttpClient();
Builder request = HttpRequest.newBuilder();
request.uri(URI.create(url));
request.DELETE();
HttpResponse res =
client.send(request.build(),
BodyHandlers.ofString());
System.out.println(res.body());
}
}