[[oktatas:programozás:java:java_rest_api_kliens|< Java REST API kliens]]
====== Java REST API Gson és HttpClient ======
* **Szerző:** Sallai András
* Copyright (c) 2023, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== A HttpClient =====
A **java.net.http.HttpClient** osztály a Java 11-ben lett bevezetve. Előtte a **java.net.HttpUrlConnection** osztályt használtuk.
A HttpClient osztály használata rövidebb kódot eredményez.
===== Szinkron =====
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class App {
public static void main(String[] args) throws Exception {
String text = getEmployees();
printName(text);
}
public static String getEmployees() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri( URI.create("http://[::1]:3000/employees"))
.build();
HttpResponse response = client.send(request, BodyHandlers.ofString());
String text = response.body();
return text;
}
public static void printName(String text) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
Employee[] employeeArray = gson.fromJson(text, Employee[].class);
ArrayList list = new ArrayList<>(Arrays.asList(employeeArray));
for(Employee employee: list) {
System.out.println(employee.name);
}
}
}
===== Aszinkron =====
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class App {
public static void main(String[] args) throws Exception {
String text = getEmployees().join();
printNames(text);
}
public static CompletableFuture getEmployees() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri( URI.create("http://[::1]:3000/employees"))
.build();
return client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body);
}
public static void printNames(String text) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
Employee[] employeeArray = gson.fromJson(text, Employee[].class);
ArrayList list = new ArrayList<>(Arrays.asList(employeeArray));
for(Employee employee: list) {
System.out.println(employee.name);
}
}
}
===== Fejrész beállítása =====
request.setHeader(HttpHeaders.CONTENT_TYPE,"application/json");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
import java.net.http.HttpRequest;
//...
String token = "sdfasdfas";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://valahol"))
.GET()
.header("Authorization", "Bearer " + token)
.build();