Commit cc0634a3 by Patryk Czarnik

Klient RESTEasy - wersja JSON

parent 41a0b40d
......@@ -9,6 +9,7 @@
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resteasy.version>6.2.4.Final</resteasy.version>
</properties>
<build>
......@@ -36,12 +37,17 @@
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.2.4.Final</version>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>6.2.4.Final</version>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
</dependencies>
</project>
package sklep.klient_rest;
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 P08_RestClient_JSON_JedenProdukt {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
Response response = client.target(Ustawienia.ADRES_USLUGI)
.path("products.json")
.path("1")
.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());
Product product = response.readEntity(Product.class);
System.out.println("Odczytany produkt: " + product);
}
}
package sklep.klient_rest;
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;
import sklep.model.ProductList;
public class P08_RestClient_JSON_Lista {
public static void main(String[] args) {
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());
GenericType<List<Product>> typListy = new GenericType<List<Product>>() {};
List<Product> products = response.readEntity(typListy);
for(Product product : products) {
System.out.println(product);
}
}
}
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