[[oktatas:programozás:java:java_rest_api_kliens|< Java REST API kliens]] ====== Java REST API kliens - resclient ====== * **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 ===== Maven ===== hu.szit resclient 1.3.2 Ha moduláris alkalmazást készítünk, a module-info.java fájlba: module valami { requires hu.szit.resclient; opens com.example to hu.szit.resclient; } GitHub weblap: * https://github.com/andteki/resclient ===== CRUD műveletek aszinkron módban ===== import hu.szit.ResClientAsync; public class UseResclient { public static void get() { String url = "https://jsonplaceholder.typicode.com/users"; ResClientAsync client = new ResClientAsync(); String res = client.get(url).join(); System.out.println(res); } public static void post() { String url = "https://jsonplaceholder.typicode.com/users"; ResClientAsync client = new ResClientAsync(); String body = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; String res = client.post(url, body).join(); System.out.println(res); } public static void put() { String url = "https://jsonplaceholder.typicode.com/users/1"; ResClientAsync client = new ResClientAsync(); String body = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; String res = client.put(url, body).join(); System.out.println(res); } public static void delete() { String url = "https://jsonplaceholder.typicode.com/users/1"; ResClientAsync client = new ResClientAsync(); String res = client.delete(url).join(); System.out.println(res); } } ===== CRUD műveletek szinkron módban ===== import hu.szit.ResClient; public class UseResclient { public static void get() { String url = "https://jsonplaceholder.typicode.com/users"; ResClient client = new ResClient(); String res = client.get(url); System.out.println(res); } public static void post() { String url = "https://jsonplaceholder.typicode.com/users"; ResClient client = new ResClient(); String body = "{ \"name\": \"John\", \"age\": 30 }"; String res = client.post(url, body); System.out.println(res); } public static void put() { String url = "https://jsonplaceholder.typicode.com/users/1"; ResClient client = new ResClient(); String body = "{ \"name\": \"John\", \"age\": 30 }"; String res = client.put(url, body); System.out.println(res); } public static void delete() { String url = "https://jsonplaceholder.typicode.com/users/1"; ResClient client = new ResClient(); String res = client.delete(url); System.out.println(res); } }