[[oktatas:programozás:java:java_rest_api_kliens|< Java REST API kliens]]
====== Java REST API Unirest és Gson használatával ======
* **Szerző:** Sallai András
* Copyright (c) 2024, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== Az Unirest-ről =====
Az Unirest számára minimum 11 Java szükséges. A háttérben az Apache HttpClient megvalósítást használja.
Az Unirest elérhetősége:
* https://github.com/Kong/unirest-java (2024)
* https://kong.github.io/unirest-java/ (2024)
==== JAR függőségek ====
Ha jar fájlokat használunk a következőkre van szükség:
* commons-logging
* httpasyncclient
* httpclient
* httpcore
* httpcore-nio
* unirest-java
===== Maven =====
com.konghq
unirest-java-bom
4.4.0
pom
import
com.konghq
unirest-java-core
com.konghq
unirest-modules-gson
com.konghq
unirest-modules-jackson
===== Példa GET metódusra =====
package lan.zold;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
public class Main {
public static void main(String[] args) {
String url = "https://jsonplaceholder.typicode.com/todos";
HttpResponse apiResponse = Unirest.get(url).asJson();
System.out.println(apiResponse.getBody().toPrettyString());
}
}
===== Helyi URL =====
Ha hai-server-t használunk helyettesítő szervernek szükség lehet a localhost helyett: [::1] névre:
package lan.zold;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
public class Main {
public static void main(String[] args) {
String url = "http://[::1]:8000/employees";
HttpResponse apiResponse = Unirest.get(url)
.asJson();
System.out.println(apiResponse.getBody().toPrettyString());
}
}
===== CRUD műveletek =====
package lan.zold;
public class Employee {
Integer id;
String name;
String city;
Double salary;
public Employee(Integer id, String name, String city, Double salary) {
this.id = id;
this.name = name;
this.city = city;
this.salary = salary;
}
public Employee(String name, String city, Double salary) {
this.name = name;
this.city = city;
this.salary = salary;
}
}
package lan.zold;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
public class Api {
String url = "http://[::1]:8000/employees";
public ArrayList getEmployees() {
HttpResponse apiResponse =
Unirest.get(url).asJson();
String json = apiResponse.getBody().toString();
Gson gson = new Gson();
Employee[] employeeArray =
gson.fromJson(json, Employee[].class);
ArrayList employeeList =
new ArrayList<>(Arrays.asList(employeeArray));
return employeeList;
}
public String createEmployee(Employee employee) {
Gson gson = new Gson();
String json = gson.toJson(employee);
HttpResponse apiResponse = Unirest.post(url)
.header("Content-Type", "application/json")
.body(json)
.asString();
return apiResponse.getBody();
}
public String updateEmployee(Employee employee) {
Gson gson = new Gson();
String json = gson.toJson(employee);
HttpResponse apiResponse =
Unirest.put(url + "/" + employee.id)
.header("Content-Type", "application/json")
.body(json)
.asString();
return apiResponse.getBody();
}
public String deleteEmployee(Integer id) {
HttpResponse apiResponse =
Unirest.delete(url + "/" + id)
.asString();
return apiResponse.getBody();
}
}
package lan.zold;
import java.util.ArrayList;
public class Main {
static Api api = new Api();
public static void main(String[] args) {
getEmployees();
}
public static void getEmployees() {
ArrayList employees = api.getEmployees();
for (Employee employee : employees) {
System.out.println(employee.name);
}
}
public static void createEmployee() {
Employee employee = new Employee(
"John",
"London",
1000.0);
System.out.println(api.createEmployee(employee));
}
public static void updateEmployee() {
Employee employee = new Employee(4, "Árpád", "Szeged", 500.0);
System.out.println(api.updateEmployee(employee));
}
public static void deleteEmployee() {
System.out.println(api.deleteEmployee(5));
}
}