[[oktatas:programozás:java:java_rest_api_kliens|< Java REST API kliens]] ====== Java REST API HttpClient példa ====== * **Szerző:** Sallai András * Copyright (c) 2023, Sallai András * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== 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 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 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 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 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 ===== 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 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjkyODg2MjQ4LCJleHAiOjE2OTI5NzI2NDh9.MeRPZfu79yS3g23TDOMbSeloPVXxX44xHIaB8RrJ3aU"; 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 ===== 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 ===== 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 ===== public class App { public static void main(String[] args) throws Exception { new Man(); } } ===== 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 post(String url, String body, String... token) { HttpClient client = HttpClient.newHttpClient(); List 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); }