Commit 3658628b by Patryk Czarnik

Poprawki przykładów RestKlient (bez modelu)

parent 7bb05898
...@@ -9,7 +9,7 @@ public class Klient01_URL { ...@@ -9,7 +9,7 @@ public class Klient01_URL {
public static void main(String[] args) { public static void main(String[] args) {
// Najprostszy sposób w Javie, aby pobrać dane z adresu URL, to użyć klasy URL. // Najprostszy sposób w Javie, aby pobrać dane z adresu URL, to użyć klasy URL.
try { try {
URL url = new URL("http://localhost:8080/PC35-RestSerwer/products.json"); URL url = new URL("http://localhost:8080/PC27-RestSerwer/api/products.json/1");
System.out.println("Odczytuję dane..."); System.out.println("Odczytuję dane...");
try (InputStream input = url.openStream()) { try (InputStream input = url.openStream()) {
// teraz z inputa możemy czytać ciąg bajtów // teraz z inputa możemy czytać ciąg bajtów
......
...@@ -4,25 +4,26 @@ import java.io.IOException; ...@@ -4,25 +4,26 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import jakarta.json.Json; import jakarta.json.*;
import jakarta.json.JsonArray;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
public class Klient02_URL_JSON { public class Klient02_URL_JSON {
/* W tej wersji pobieramy dane JSON podstawowym sposobem URL, ale pobrane dane parsujemy za pomocą narzędzi
z biblioteki Jakarta JSON P - w tym projekcie dostępnej dzięki zależności eclipse parsson.
*/
public static void main(String[] args) { public static void main(String[] args) {
try { try {
URL url = new URL(Ustawienia.ADRES_USLUGI + "/products.json"); URL url = new URL(Ustawienia.ADRES_USLUGI + "/products.json");
try(InputStream inputStream = url.openStream(); try(InputStream inputStream = url.openStream();
JsonReader reader = Json.createReader(inputStream)) { JsonReader reader = Json.createReader(inputStream)) {
// pierwszym znakiem w odpowiedzi jest nawias kwadratowy, czyli należy wczytać tablicę jsonową
JsonArray array = reader.readArray(); JsonArray array = reader.readArray();
// System.out.println(array); // System.out.println(array);
for(JsonValue jsonValue : array) { for(JsonValue jsonValue : array) {
//System.out.println(jsonValue); //System.out.println(jsonValue);
System.out.println(jsonValue.asJsonObject().getString("productName")); JsonObject jsonObject = jsonValue.asJsonObject();
System.out.println(" opis: " + jsonValue.asJsonObject().getString("description", "")); System.out.println(jsonObject.getString("productName"));
System.out.println(" cena: " + jsonValue.asJsonObject().getJsonNumber("price").bigDecimalValue()); System.out.println(" opis: " + jsonObject.getString("description", ""));
System.out.println(" cena: " + jsonObject.getJsonNumber("price").bigDecimalValue());
} }
} }
} catch (IOException e) { } catch (IOException e) {
......
...@@ -19,8 +19,7 @@ public class Klient03_HttpClient { ...@@ -19,8 +19,7 @@ public class Klient03_HttpClient {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
HttpClient httpClient = HttpClient.newHttpClient(); try(HttpClient httpClient = HttpClient.newHttpClient()) {
try {
URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products.json"); URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products.json");
HttpRequest request = HttpRequest.newBuilder(uri).build(); HttpRequest request = HttpRequest.newBuilder(uri).build();
HttpResponse<Path> response = httpClient.send(request, BodyHandlers.ofFile(Paths.get("wynik03.json"))); HttpResponse<Path> response = httpClient.send(request, BodyHandlers.ofFile(Paths.get("wynik03.json")));
......
...@@ -11,8 +11,7 @@ import java.net.http.HttpResponse.BodyHandlers; ...@@ -11,8 +11,7 @@ import java.net.http.HttpResponse.BodyHandlers;
public class Klient04_HttpClient_String { public class Klient04_HttpClient_String {
public static void main(String[] args) { public static void main(String[] args) {
HttpClient httpClient = HttpClient.newHttpClient(); try(HttpClient httpClient = HttpClient.newHttpClient()) {
try {
URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products.json"); URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products.json");
HttpRequest request = HttpRequest.newBuilder(uri).build(); HttpRequest request = HttpRequest.newBuilder(uri).build();
// Body z odpowiedzi pobierzemy jako obiekt String z całą treścią // Body z odpowiedzi pobierzemy jako obiekt String z całą treścią
...@@ -20,7 +19,8 @@ public class Klient04_HttpClient_String { ...@@ -20,7 +19,8 @@ public class Klient04_HttpClient_String {
System.out.println("response " + response); System.out.println("response " + response);
System.out.println("status: " + response.statusCode()); System.out.println("status: " + response.statusCode());
System.out.println("Content-Type: " + response.headers().firstValue("Content-Type").orElse("BRAK")); System.out.println("Content-Type: " + response.headers().firstValue("Content-Type").orElse("BRAK"));
System.out.println("Treść odpowiedzi:\n" + response.body()); // tutaj String body = response.body();
System.out.println("Treść odpowiedzi:\n" + body);
} catch (URISyntaxException | IOException | InterruptedException e) { } catch (URISyntaxException | IOException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -11,12 +11,11 @@ import java.net.http.HttpResponse.BodyHandlers; ...@@ -11,12 +11,11 @@ import java.net.http.HttpResponse.BodyHandlers;
public class Klient05_HttpClient_Accept { public class Klient05_HttpClient_Accept {
public static void main(String[] args) { public static void main(String[] args) {
HttpClient httpClient = HttpClient.newHttpClient(); try(HttpClient httpClient = HttpClient.newHttpClient()) {
try {
URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products"); URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products");
// W tej wersji do zapytania dodajemy nagłówek Accept // W tej wersji do zapytania dodajemy nagłówek Accept
HttpRequest request = HttpRequest.newBuilder(uri) HttpRequest request = HttpRequest.newBuilder(uri)
.header("Accept", "text/plain") .header("Accept", "text/plain") // albo application/xml, application/json, text/html
.build(); .build();
HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString()); HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString());
System.out.println("response " + response); System.out.println("response " + response);
......
...@@ -22,8 +22,7 @@ public class Klient06_HttpClient_JSON { ...@@ -22,8 +22,7 @@ public class Klient06_HttpClient_JSON {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
HttpClient httpClient = HttpClient.newHttpClient(); try(HttpClient httpClient = HttpClient.newHttpClient()) {
try {
URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products/1"); URI uri = new URI(Ustawienia.ADRES_USLUGI + "/products/1");
HttpRequest request = HttpRequest.newBuilder(uri) HttpRequest request = HttpRequest.newBuilder(uri)
.header("Accept", "application/json") .header("Accept", "application/json")
......
...@@ -34,14 +34,15 @@ public class Klient07_HttpClient_JSON_Lista { ...@@ -34,14 +34,15 @@ public class Klient07_HttpClient_JSON_Lista {
} }
private static JsonArray pobierzJsona(String adres) throws IOException, InterruptedException, URISyntaxException { private static JsonArray pobierzJsona(String adres) throws IOException, InterruptedException, URISyntaxException {
HttpClient httpClient = HttpClient.newHttpClient(); try(HttpClient httpClient = HttpClient.newHttpClient()) {
URI uri = new URI(adres); URI uri = new URI(adres);
HttpRequest request = HttpRequest.newBuilder(uri).build(); HttpRequest request = HttpRequest.newBuilder(uri).build();
HttpResponse<InputStream> response = httpClient.send(request, BodyHandlers.ofInputStream()); HttpResponse<InputStream> response = httpClient.send(request, BodyHandlers.ofInputStream());
System.out.println("response " + response); System.out.println("response " + response);
System.out.println("status: " + response.statusCode()); System.out.println("status: " + response.statusCode());
System.out.println("Content-Type: " + response.headers().firstValue("Content-Type").orElse("BRAK")); System.out.println("Content-Type: " + response.headers().firstValue("Content-Type").orElse("BRAK"));
return wczytajJsona(response.body()); return wczytajJsona(response.body());
}
} }
private static JsonArray wczytajJsona(InputStream input) { private static JsonArray wczytajJsona(InputStream input) {
......
package rest_klient; package rest_klient;
public class Ustawienia { public class Ustawienia {
public static final String ADRES_USLUGI = "http://localhost:8080/PC27-RestSerwer"; public static final String ADRES_USLUGI = "http://localhost:8080/PC27-RestSerwer/api";
} }
import requests
dane = requests.get("http://localhost:8080/PC27-RestSerwer/api/products.json").json()
print("Liczba elementów", len(dane))
for produkt in dane:
print(f'Produckt o nazwie {produkt["productName"]} ma cenę {produkt["price"]}')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment