oktatas:programozas:java:java_rest_api_kliens:httpclient
Tartalomjegyzék
Java REST API kliens - HttpClient
- Szerző: Sallai András
- Copyright © 2024, Sallai András
- Szerkesztve: 2024
- Licenc: CC BY-SA 4.0
- Web: https://szit.hu
CRUD műveletek aszinkron módban
- ClientAsync.java
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<String> 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<String> 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<String> 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<String> res = client.sendAsync(request.build(), BodyHandlers.ofString()) .thenApply(HttpResponse::body); System.out.println(res.join()); } }
CRUD műveletek szinkron módban
- Client.java
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<String> 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<String> 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<String> 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<String> res = client.send(request.build(), BodyHandlers.ofString()); System.out.println(res.body()); } }
oktatas/programozas/java/java_rest_api_kliens/httpclient.txt · Utolsó módosítás: 2024/11/01 12:38 szerkesztette: admin