Commit f8ef5b6d by Patryk Czarnik

Klient obiektowy

parent 02bb20c1
...@@ -32,3 +32,6 @@ build/ ...@@ -32,3 +32,6 @@ build/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
/wynik03.json /wynik03.json
/wynik11.json
/product0001.pdf
/wynik13.txt
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target> <maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resteasy.version>6.2.12.Final</resteasy.version>
</properties> </properties>
<dependencies> <dependencies>
...@@ -31,7 +32,17 @@ ...@@ -31,7 +32,17 @@
<dependency> <dependency>
<groupId>org.jboss.resteasy</groupId> <groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId> <artifactId>resteasy-client</artifactId>
<version>6.2.12.Final</version> <version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${resteasy.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -8,7 +8,7 @@ public class Klient12_RestClient_String { ...@@ -8,7 +8,7 @@ public class Klient12_RestClient_String {
public static void main(String[] args) { public static void main(String[] args) {
try(Client client = ClientBuilder.newClient()) { try(Client client = ClientBuilder.newClient()) {
// Taki styl programowania to "fluent API". Przy okazji - zarówno Client, jak i Response są "zamykalne. // Taki styl programowania to "fluent API". Przy okazji - zarówno Client, jak i Response są "zamykalne".
try(Response response = client.target(Ustawienia.ADRES_USLUGI) try(Response response = client.target(Ustawienia.ADRES_USLUGI)
.path("products.json") .path("products.json")
.request() .request()
......
...@@ -24,7 +24,8 @@ public class Klient14_RestClient_PDF { ...@@ -24,7 +24,8 @@ public class Klient14_RestClient_PDF {
WebTarget root = client.target(Ustawienia.ADRES_USLUGI); WebTarget root = client.target(Ustawienia.ADRES_USLUGI);
try(Response response = root try(Response response = root
.path("products") .path("products.pdf")
//.path("products")
.path("{id}") .path("{id}")
.resolveTemplate("id", productId) .resolveTemplate("id", productId)
.request() .request()
......
package sklep.klient;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Response;
import sklep.model.Product;
import sklep.model.ProductList;
public class Klient21_RestClient_JAXB {
public static void main(String[] args) {
try (Client client = ClientBuilder.newClient();
Response response = client.target(Ustawienia.ADRES_USLUGI)
.path("products.xml")
.request()
.buildGet()
.invoke()) {
System.out.println("Mam odpowiedź: " + response);
System.out.println("Status: " + response.getStatus());
System.out.println("C-Type: " + response.getMediaType());
System.out.println("Length: " + response.getLength());
ProductList products = response.readEntity(ProductList.class);
System.out.println("Otrzymane dane:");
for (Product product : products.getProducts()) {
System.out.println(product);
}
}
}
}
package sklep.klient;
import java.util.List;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.Response;
import sklep.model.Product;
public class Klient22_RestClient_JSON {
public static void main(String[] args) {
try(Client client = ClientBuilder.newClient();
Response response = client.target(Ustawienia.ADRES_USLUGI)
.path("products.json")
.request()
.buildGet()
.invoke()) {
System.out.println("Mam odpowiedź: " + response);
System.out.println("Status: " + response.getStatus());
System.out.println("C-Type: " + response.getMediaType());
System.out.println("Length: " + response.getLength());
// Ponieważ wersja JSON na serwerze zwraca wynik typu List<Product>, to tutaj musimy podać "typ generyczny",
// a nie wystarczy zwykła klasa.
// Nie zadziała:
// List<Product> products = response.readEntity(List.class);
GenericType<List<Product>> typListy = new GenericType<>() {
};
List<Product> products = response.readEntity(typListy);
// albo jednolinijkowo:
// List<Product> products = response.readEntity(new GenericType<List<Product>>() {});
for (Product product : products) {
System.out.println(product);
}
}
}
}
package sklep.klient;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import sklep.model.Product;
import java.util.Scanner;
public class Klient23_RestClient_JSON_JedenProdukt {
public static void main(String[] args) {
try(Client client = ClientBuilder.newClient();
Scanner scanner = new Scanner(System.in)) {
final WebTarget path = client.target(Ustawienia.ADRES_USLUGI)
.path("products")
.path("{id}");
while (true) {
System.out.print("Podaj id produktu (-1 kończy): ");
int id = Integer.parseInt(scanner.nextLine());
if (id == -1) {
break;
}
try (Response response = path
.resolveTemplate("id", id)
.request()
.accept(MediaType.APPLICATION_JSON)
.buildGet()
.invoke()) {
System.out.println("Mam odpowiedź: " + response);
System.out.println("Status: " + response.getStatus());
System.out.println("C-Type: " + response.getMediaType());
System.out.println("Length: " + response.getLength());
Product product = response.readEntity(Product.class);
System.out.println("Odczytany produkt: " + product);
}
}
}
}
}
package sklep.klient;
import java.math.BigDecimal;
import java.util.Scanner;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import sklep.model.Product;
public class Klient24_Interaktywna_Edycja {
public static void main(String[] args) {
System.out.println("Startujemy...");
try (Scanner scanner = new Scanner(System.in);
Client client = ClientBuilder.newClient()) {
WebTarget path = client.target(Ustawienia.ADRES_USLUGI)
.path("products")
.path("{id}");
System.out.println("Przygotowana ścieżka: " + path);
while (true) {
System.out.print("\nPodaj id: ");
int id = scanner.nextInt();
if (id == 0) break;
try(Response response = path
.resolveTemplate("id", id)
.request(MediaType.APPLICATION_JSON)
.get()) {
System.out.println("Status: " + response.getStatus());
System.out.println("Content-Type: " + response.getMediaType());
if (response.getStatus() == 200) {
Product product = response.readEntity(Product.class);
System.out.println("Mam produkt:");
System.out.println(" Nazwa: " + product.getProductName());
System.out.println(" Cena: " + product.getPrice());
System.out.println(" Opis: " + product.getDescription());
System.out.println();
System.out.println("Podaj zmianę ceny (0 aby nie zmieniać):");
BigDecimal zmianaCeny = scanner.nextBigDecimal();
if (zmianaCeny.compareTo(BigDecimal.ZERO) != 0) {
BigDecimal newPrice = product.getPrice().add(zmianaCeny);
System.out.println("PUT nowej ceny...");
try(Response odpPut = path.path("price").resolveTemplate("id", id).request()
.put(Entity.entity(newPrice, MediaType.TEXT_PLAIN_TYPE))) {
System.out.println("PUT zakończył się kodem " + odpPut.getStatus());
}
}
} else {
System.out.println("nie mogę odczytać");
}
}
}
}
}
}
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