Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
2224c256
Commit
2224c256
authored
Nov 09, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe metody do HTML
parent
21459e8a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
2 deletions
+36
-2
RProducts.java
PC35-RestSerwer/src/main/java/rest/RProducts.java
+36
-2
No files found.
PC35-RestSerwer/src/main/java/rest/RProducts.java
View file @
2224c256
...
@@ -16,19 +16,45 @@ import sklep.db.DBException;
...
@@ -16,19 +16,45 @@ import sklep.db.DBException;
import
sklep.db.ProductDAO
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.model.ProductList
;
@Path
(
"/products"
)
@Path
(
"/products"
)
public
class
RProducts
{
public
class
RProducts
{
@GET
@GET
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
@Produces
({
"application/xml"
,
"text/plain"
})
public
List
<
Product
>
readAll
()
throws
DBException
{
public
ProductList
readAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
new
ProductList
(
productDAO
.
readAll
());
}
}
// Żeby w JSON nie było dodatkowego poziomu w strukturze, zwracam bezpośrednio listę rekordów:
@GET
@Produces
({
"application/json"
})
public
List
<
Product
>
readAllJSON
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
();
return
productDAO
.
readAll
();
}
}
}
}
// 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
readAllHTML
()
throws
DBException
{
List
<
Product
>
products
=
readAll
().
getProducts
();
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
();
}
@GET
@GET
@Path
(
"/{id}"
)
@Path
(
"/{id}"
)
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
...
@@ -39,6 +65,14 @@ public class RProducts {
...
@@ -39,6 +65,14 @@ public class RProducts {
}
}
}
}
@GET
@Produces
(
"text/html;charset=UTF-8"
)
@Path
(
"/{id}"
)
public
String
readOneHTML
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
Product
product
=
readOne
(
productId
);
return
"<!DOCTYPE html>\n<html><body>"
+
product
.
toHtml
()
+
"</body></html>"
;
}
@POST
@POST
@Consumes
({
"application/json"
,
"application/xml"
})
@Consumes
({
"application/json"
,
"application/xml"
})
// W metodach typu POST i PUT powinien znajdować się dokładnie jeden parametr nieozanczony żadną adnotacją.
// W metodach typu POST i PUT powinien znajdować się dokładnie jeden parametr nieozanczony żadną adnotacją.
...
...
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