Commit 8fb7b555 by Patryk Czarnik

Adnotacje JAX-WS i JAXB, aby zcustomizować XMLa

parent 4c17a0b0
package sklep.model;
import java.time.LocalDateTime;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdapterDaty extends XmlAdapter<String, LocalDateTime> {
@Override
public String marshal(LocalDateTime d) {
return d.toString();
}
@Override
public LocalDateTime unmarshal(String s) {
return LocalDateTime.parse(s);
}
}
......@@ -2,11 +2,15 @@ package sklep.model;
import java.util.Objects;
import javax.xml.bind.annotation.XmlElement;
public class Customer {
private String email;
private String name;
@XmlElement(name="phone")
private String phoneNumber;
private String address;
@XmlElement(name="postal-code")
private String postalCode;
private String city;
......
......@@ -7,11 +7,27 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class Order {
@XmlAttribute(name="id")
private Integer orderId;
@XmlElement(name="customer-email")
private String customerEmail;
@XmlElement(name="order-date")
@XmlJavaTypeAdapter(AdapterDaty.class)
private LocalDateTime orderDate;
@XmlAttribute(name="status")
private Status orderStatus;
@XmlElementWrapper(name="products")
@XmlElement(name="product")
public final List<OrderProduct> products = new ArrayList<>();
public Order() {
......
......@@ -3,8 +3,18 @@ package sklep.model;
import java.math.BigDecimal;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
/* Technologia JAXB jest odpowiedzialna za automatyczne tłumaczenie obiektów Javy do postaci XML i odwrotnie.
* To działa w oparciu o adnotacje, których większośc rozpoczyna się od @Xml....
* Za pomocą takich adnotacji możemy wpłynąć na postać XML (nazwy, kolejność, sposób konwersji).
*/
public class Product {
@XmlAttribute(name="id")
private Integer productId;
@XmlElement(name="name")
private String productName;
private BigDecimal price;
private BigDecimal vat;
......@@ -30,7 +40,7 @@ public class Product {
}
public String getProductName() {
return productName;
return "blablabla " + productName.toUpperCase() +"beleble";
}
public void setProductName(String productName) {
......
@XmlAccessorType(XmlAccessType.FIELD)
package sklep.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
......@@ -3,6 +3,8 @@ package sklep.soap;
import java.math.BigDecimal;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import sklep.db.CustomerDAO;
......@@ -17,7 +19,7 @@ import sklep.model.Product;
@WebService
public class Sklep {
@WebResult(name="product")
public List<Product> odczytajWszystkieProdukty() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
......@@ -25,20 +27,26 @@ public class Sklep {
}
}
public List<Product> produktyWgCeny(BigDecimal min, BigDecimal max) throws DBException {
@WebResult(name="product")
public List<Product> produktyWgCeny(
@WebParam(name="min") BigDecimal minPrice,
@WebParam(name="max") BigDecimal maxPrice
) throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.findByPrice(min, max);
return productDAO.findByPrice(minPrice, maxPrice);
}
}
public Product odczytajJedenProdukt(int productId) throws DBException, RecordNotFound {
@WebResult(name="product")
public Product odczytajJedenProdukt(@WebParam(name="id") int productId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.findById(productId);
}
}
@WebResult(name="order")
public List<Order> odczytajWszystkieZamowienia() throws DBException {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
......@@ -46,13 +54,15 @@ public class Sklep {
}
}
public Order odczytajJednoZamowienie(int orderId) throws DBException, RecordNotFound {
@WebResult(name="order")
public Order odczytajJednoZamowienie(@WebParam(name="id") int orderId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
return orderDAO.findById(orderId);
}
}
@WebResult(name="customer")
public List<Customer> odczytajWszystkichKlientow() throws DBException {
try(DBConnection db = DBConnection.open()) {
CustomerDAO customerDAO = db.customerDAO();
......@@ -60,7 +70,8 @@ public class Sklep {
}
}
public Customer odczytajJednegoKlienta(String email) throws DBException, RecordNotFound {
@WebResult(name="customer")
public Customer odczytajJednegoKlienta(@WebParam(name="email") String email) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
CustomerDAO customerDAO = db.customerDAO();
return customerDAO.findByEmail(email);
......
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