Commit 66e3cfbe by Patryk Czarnik

Przykłady klienta REST wykorzystujące mapowanie na obiekty

parent a01a67cf
package sklep.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import sklep.model.ProductBezAdnotacji;
public class Klient8_JSON {
public static void main(String[] args) {
System.out.println("Startujemy...");
Client client = ClientBuilder.newClient();
WebTarget root = client.target(Ustawienia.URL_SERWERA);
Response response = root
.path("products/1")
.request()
.accept(MediaType.APPLICATION_JSON)
.buildGet()
.invoke();
System.out.println("Status: " + response.getStatus());
System.out.println("Content-Type: " + response.getMediaType());
if(response.getStatus() != 200) {
System.out.println("Chyba coś nie tak, więc przerywam.");
return;
}
ProductBezAdnotacji product = response.readEntity(ProductBezAdnotacji.class);
System.out.println("Mam produkt: " + product);
}
}
package sklep.klient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import sklep.model.Product;
public class Klient9_JAXB {
public static void main(String[] args) {
System.out.println("Startujemy...");
Client client = ClientBuilder.newClient();
WebTarget root = client.target(Ustawienia.URL_SERWERA);
Response response = root
.path("products/1")
.request()
.accept(MediaType.APPLICATION_XML)
.buildGet()
.invoke();
System.out.println("Status: " + response.getStatus());
System.out.println("Content-Type: " + response.getMediaType());
if(response.getStatus() != 200) {
System.out.println("Chyba coś nie tak, więc przerywam.");
return;
}
Product product = response.readEntity(Product.class);
System.out.println("Mam produkt: " + product);
System.out.println("Cena: " + product.getPrice());
}
}
package sklep.klient;
import java.math.BigDecimal;
import java.util.Scanner;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import sklep.model.Product;
public class Klient_Interaktywny {
public static void main(String[] args) {
System.out.println("Startujemy...");
Scanner scanner = new Scanner(System.in);
Client client = ClientBuilder.newClient();
WebTarget path = client.target(Ustawienia.URL_SERWERA)
.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;
Response response = path
.resolveTemplate("id", id)
.request(MediaType.APPLICATION_XML)
.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...");
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ć");
}
}
}
}
package sklep.model;
import java.math.BigDecimal;
import java.util.Objects;
// W tej wersji nie ma adnotacji JAXB (@Xml...), aby nie wpływały na działanie JSONa.
public class ProductBezAdnotacji {
private Integer productId;
private String productName;
private BigDecimal price;
private BigDecimal vat;
private String description;
public ProductBezAdnotacji() {
}
public ProductBezAdnotacji(Integer productId, String productName, BigDecimal price, BigDecimal vat, String description) {
this.productId = productId;
this.productName = productName;
this.price = price;
this.vat = vat;
this.description = description;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getVat() {
return vat;
}
public void setVat(BigDecimal vat) {
this.vat = vat;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
return Objects.hash(description, price, vat, productId, productName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductBezAdnotacji other = (ProductBezAdnotacji) obj;
return Objects.equals(productId, other.productId) && Objects.equals(productName, other.productName)
&& Objects.equals(price, other.price)
&& Objects.equals(vat, other.vat)
&& Objects.equals(description, other.description);
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", price=" + price + ", vat=" + vat
+ ", description=" + description + "]";
}
public String toHtml() {
return String.format("<div class='product'>"
+ "<h2>%s</h2>"
+ "<p>(nr %d)</p>"
+ "<p>Cena: <strong>%,.2f PLN</strong></p>"
+ "<p>%s</p>"
+ "</div>",
getProductName(),
getProductId(),
getPrice(),
getDescription());
}
}
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