Tartalomjegyzék

< Java REST API kliens

Java REST API Unirest és Gson használatával

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:

JAR függőségek

Ha jar fájlokat használunk a következőkre van szükség:

Maven

pom.xml
    <dependencyManagement>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-bom --> 
      <dependency>
          <groupId>com.konghq</groupId>
          <artifactId>unirest-java-bom</artifactId>
          <version>4.4.0</version>
          <type>pom</type>
          <scope>import</scope>
      </dependency>
  </dependencies>
</dependencyManagement>
 
<dependencies>
    <!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-core -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java-core</artifactId>
    </dependency>
 
    <!-- pick a JSON module if you want to parse JSON include one of these: -->
    <!-- Google GSON -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-modules-gson</artifactId>
    </dependency>
 
    <!-- OR maybe you like Jackson better? -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-modules-jackson</artifactId>
    </dependency>
</dependencies>

Példa GET metódusra

src/main/java/lan/zold/Main.java
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<JsonNode> 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:

src/main/java/lan/zold/Main.java
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<JsonNode> apiResponse = Unirest.get(url)
            .asJson();
 
        System.out.println(apiResponse.getBody().toPrettyString());
    }
}

CRUD műveletek

src/main/java/lan/zold/Employee.java
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;
  }
}
src/main/java/lan/zold/Api.java
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<Employee> getEmployees() {    
    HttpResponse<JsonNode> apiResponse = 
    Unirest.get(url).asJson();
    String json = apiResponse.getBody().toString();
    Gson gson = new Gson();
    Employee[] employeeArray = 
    gson.fromJson(json, Employee[].class);
    ArrayList<Employee> employeeList = 
    new ArrayList<>(Arrays.asList(employeeArray));
    return employeeList;
  }
 
  public String createEmployee(Employee employee) {
    Gson gson = new Gson();
    String json = gson.toJson(employee);
    HttpResponse<String> 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<String> apiResponse = 
    Unirest.put(url + "/" + employee.id)
        .header("Content-Type", "application/json")
        .body(json)
        .asString();
    return apiResponse.getBody();
  }
 
  public String deleteEmployee(Integer id) {
    HttpResponse<String> apiResponse = 
    Unirest.delete(url + "/" + id)
        .asString();
    return apiResponse.getBody();
  }
}
src/main/java/lan/zold/Main.java
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<Employee> 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));
    }
}