Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_dzienna_15_09
Commits
05956ec8
Commit
05956ec8
authored
Oct 07, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Metody produkujące HTML
parent
da548994
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
6 deletions
+42
-6
RProducts.java
PC34-RestSerwer/src/main/java/sklep/rest/RProducts.java
+42
-6
No files found.
PC34-RestSerwer/src/main/java/sklep/rest/RProducts.java
View file @
05956ec8
...
...
@@ -26,9 +26,15 @@ import sklep.photo.PhotoUtil;
@Path
(
"/products"
)
public
class
RProducts
{
// Gdy w adnotacji @Produces umieszczona jest lista content-typów,
// to serwer jest w stanie wygenerować odpowiedź w każdym z tych formatów.
// Jeśli w zapytaniu znajduje się nagłówek Accept, to znaczy, że klient prosi o podany format (lub kilka)
// i serwer stara się dostosować do tych preferencji.
// Jeśli nie ma nagłówka Accept, to standard JAX-RS nie mówi co ma się stać,
// ale implementacja REST Easy (WildFly/JBoss) zastosuje pierwszy z formatów podanych w adnmotacji.
// Glassfish i WebLogic tak nie robią - wybierają "losowo".
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
,
"text/plain"
}
)
public
List
<
Product
>
readAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -36,8 +42,24 @@ public class RProducts {
}
}
// W ramach JAX-RS można utworzyć wiele metod, które działają pod tym samym adresem,
// ale różnią się obsługiwanymi formatami danych (Produces/Consumes).
// W zależności od preferecji klienta zostanie wywołana jedna lub druga metoda.
@GET
@Produces
(
"application/json"
)
@Produces
(
"text/html"
)
public
String
readAllHTML
()
throws
DBException
{
List
<
Product
>
products
=
readAll
();
StringBuilder
txt
=
new
StringBuilder
(
"<html><body>"
);
txt
.
append
(
"<h1>Lista produktów:</h1>"
);
for
(
Product
product
:
products
)
{
txt
.
append
(
product
.
toHtml
());
}
txt
.
append
(
"</body></html>"
);
return
txt
.
toString
();
}
@GET
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
@Path
(
"/{id}"
)
public
Product
readOne
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
@@ -46,11 +68,23 @@ public class RProducts {
}
}
@GET
@Produces
(
"text/html"
)
@Path
(
"/{id}"
)
public
String
readOneHTML
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
Product
product
=
readOne
(
productId
);
StringBuilder
txt
=
new
StringBuilder
(
"<html><body>"
);
txt
.
append
(
"<h1>Odczytany produkt:</h1>"
);
txt
.
append
(
product
.
toHtml
());
txt
.
append
(
"</body></html>"
);
return
txt
.
toString
();
}
// To twórca usługi decyduje o tym, jakie dokładnie adresy będą dostepne, nie ma obowiązku obsługiwania wszystkich,
// ale gdybyśmy chcieli dać bezpośredni doste do wybranego pola w rekordzie, to tak jak w tym przykładzie.
// Struktura adresów URL powjnna odpowiadać logicznej strukturze danych.
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"text/plain"
}
)
@Path
(
"/{id}/price"
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
@@ -65,8 +99,10 @@ public class RProducts {
// W zapytaniu PUT jest przysyłana jakaś treść (content/body/entity)
// - format, w jakim te dane są przysyłane, opisuje adnotacja @Consumes
// - metoda może mieć jeden parametr bez żadnej adnotacji i to właśnie poprzez ten prametr jest przekazywna przysłana wartość
// Gdy adnotacja @Consumes zawiera kilka formatów, to klient może przysłać dane w każdym z nich.
@PUT
@Consumes
(
"application/json"
)
@Consumes
(
{
"text/plain"
,
"application/json"
}
)
@Path
(
"/{id}/price"
)
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
@@ -83,7 +119,7 @@ public class RProducts {
// jest traktowane jako polecenie dodania nowego rekordu.
// W tej wersji w odpowiedzi odsyłany jest cały rekord Product uzupełniony o pole ID.
@POST
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"application/xml"
}
)
@Produces
(
"application/json"
)
public
Product
addProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment