oktatas:programozas:java:java_rest_api_kliens:httpclient_pelda
Tartalomjegyzék
Java REST API HttpClient példa
- Szerző: Sallai András
- Copyright © 2023, Sallai András
- Licenc: CC BY-SA 4.0
- Web: https://szit.hu
Client.java
- Client.java
import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; public class Client { public CompletableFuture<String> get(String urlStr) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(urlStr)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture<String> post(String url, String body, String... token) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .header("Authorization", "Bearer " + token[0]) .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture<String> put(String url, String body) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .PUT(HttpRequest.BodyPublishers.ofString(body)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture<String> delete(String url) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .DELETE() .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } }
Az Emp.java
- Emp.java
public class Emp { Client client; String host; String endpoint; public Emp() { this.client = new Client(); this.host = "http://[::1]:8000/api"; this.endpoint = "/employees"; } public String getEmployees() { String url = host + endpoint; String res = client.get(url).join(); return res; } public String addEmployee(String emp) { String token = "eyJhbGci"; Client client = new Client(); String url = host + endpoint; String res = client.post(url, emp, token ).join(); return res; } public String updateEmployee(String emp, Integer id) { String url = this.host + endpoint + "/" + id.toString(); String res = client.put(url, emp ).join(); return res; } public String deleteEmployee(Integer id) { String url = host + endpoint + "/" + id.toString(); String res = client.delete(url).join(); return res; } }
Man
- Man.java
public class Man { Emp emp; public Man() { this.emp = new Emp(); create(); // index(); // login(); } public void index() { String res = this.emp.getEmployees(); System.out.println(res); } public void create() { String pali1 = "{ \"name\":\"Teknős Péter\","; String pali2 = "\"city\":\"Hatvan\","; String pali3 = "\"salary\": 322 }"; String data = pali1 + pali2 + pali3; String res = this.emp.addEmployee(data); System.out.println(res); } public void update() { String pali1 = "{ \"name\":\"Csengő Mária\","; String pali2 = "\"city\":\"Pécs\","; String pali3 = "\"salary\": 349 }"; String data = pali1 + pali2 + pali3; String res = this.emp.updateEmployee(data, 4); System.out.println(res); } public void delete() { String res = this.emp.deleteEmployee(6); System.out.println(res); } public void login() { Auth auth = new Auth(); String data1 = "{ \"name\":\"mari\","; String data2 = "\"password\":\"titok\"}"; String data = data1 + data2; String res = auth.login(data); System.out.println(res); } }
Auth.java
- Auth.java
public class Auth { Client client; String host; String endpoint; public Auth() { this.client = new Client(); this.host = "http://[::1]:8000/api"; this.endpoint = "/login"; } public String login(String data) { Client client = new Client(); String url = host + endpoint; String res = client.post(url, data ).join(); return res; } }
App.java
A Client post metódusa átírva
A fejrészt csak akkor adjuk hozzá, ha át van adva a token, ami nem kötelező.
public CompletableFuture<String> post(String url, String body, String... token) { HttpClient client = HttpClient.newHttpClient(); List<String> headers = new ArrayList<>(); headers.add("Content-Type"); headers.add("application/json"); System.out.println(token.length); if(token.length > 0) { headers.add("Authorization"); headers.add("Bearer " + token[0]); } HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .headers(headers.toArray(String[]::new)) .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); }
oktatas/programozas/java/java_rest_api_kliens/httpclient_pelda.txt · Utolsó módosítás: 2024/11/01 19:47 szerkesztette: admin