Commit 1f54b347 by Patryk Czarnik

Metody zwracajce HTML

parent f52b6c86
package rest;
import java.math.BigDecimal;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
......@@ -30,7 +31,7 @@ public class RProduct {
// Gdy w Produces jest wiele formatów, to klient może wybrać za pomocą nagłówka Accept
// Gdy w Consumes jest wiele formatów, to klient może przysłać dane w dowolnym z nich (nagłówek Content-Type)
@GET
@Produces({"application/json", "application/xml", "text/plain"})
@Produces({"application/json", "application/xml", "text/plain;charset=UTF-8"})
public ProductList readAllProducts() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
......@@ -38,6 +39,24 @@ public class RProduct {
}
}
// Może też być tak, że kilka metod działa pod tym samym adresem, ale służą one do tworzenia odpowiedzi w różnych formatach.
// Przykład: tworzenie HTML w oddzielnej metodzie
@GET
@Produces("text/html;charset=UTF-8")
public String readAllProductsHTML() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
List<Product> products = productDAO.readAll();
StringBuilder txt = new StringBuilder("<!DOCTYPE html>\n<html><body>\n");
txt.append("<h1>Lista produktów</h1>\n");
for(Product product : products) {
txt.append(product.toHtml()).append('\n');
}
txt.append("</body></html>");
return txt.toString();
}
}
@Path("/{id}")
@GET
@Produces({"application/json", "application/xml", "text/plain"})
......@@ -48,6 +67,14 @@ public class RProduct {
}
}
@Path("/{id}")
@GET
@Produces("text/html;charset=UTF-8")
public String readOneProductsHTML(@PathParam("id") int productId) throws DBException, RecordNotFound {
Product product = readOneProduct(productId);
return "<!DOCTYPE html>\n<html><body>" + product.toHtml() + "</body></html>";
}
@Path("/{id}/foto")
@GET
@Produces("image/jpeg")
......
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