< Java REST API kliens

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

<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 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); } }