oktatas:programozas:java:java_rest_api_kliens:resclient
Tartalomjegyzék
Java REST API kliens - resclient
- Szerző: Sallai András
- Copyright © 2024, Sallai András
- Szerkesztve: 2024
- Licenc: CC BY-SA 4.0
- Web: https://szit.hu
Maven
- pom.xml
<dependency> <groupId>hu.szit</groupId> <artifactId>resclient</artifactId> <version>1.3.2</version> </dependency>
Ha moduláris alkalmazást készítünk, a module-info.java fájlba:
- module-info.java
module valami { requires hu.szit.resclient; opens com.example to hu.szit.resclient; }
GitHub weblap:
CRUD műveletek aszinkron módban
- UseResclient.java
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
- UseResclient.java
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); } }
oktatas/programozas/java/java_rest_api_kliens/resclient.txt · Utolsó módosítás: 2024/11/10 08:54 szerkesztette: admin