Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
1f54b347
Commit
1f54b347
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Metody zwracajce HTML
parent
f52b6c86
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
1 deletions
+28
-1
RProduct.java
PC35-RestSerwer/src/main/java/rest/RProduct.java
+28
-1
No files found.
PC35-RestSerwer/src/main/java/rest/RProduct.java
View file @
1f54b347
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"
)
...
...
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