Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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_20230617
Commits
0c10bfca
Commit
0c10bfca
authored
Aug 20, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Response.created
parent
33d9de06
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
5 deletions
+10
-5
RProducts.java
PC32-RestSerwer/src/main/java/sklep/rest/RProducts.java
+10
-5
No files found.
PC32-RestSerwer/src/main/java/sklep/rest/RProducts.java
View file @
0c10bfca
package
sklep
.
rest
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.util.List
;
import
jakarta.ws.rs.Consumes
;
...
...
@@ -11,6 +12,8 @@ import jakarta.ws.rs.PUT;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
...
...
@@ -112,13 +115,14 @@ public class RProducts {
// POST potrafi "dodać rekord do katalogu".
@POST
@Consumes
({
"application/json"
,
"application/xml"
})
@Produces
({
"application/json"
,
"application/xml"
})
public
Product
zapiszProdukt
(
Product
product
)
throws
DBException
{
public
Response
zapiszProdukt
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
return
product
;
// Pobranie adresu obsługiwanego przez klasę RProducts i dodanie id konkretnego produktu:
URI
uri
=
UriBuilder
.
fromResource
(
RProducts
.
class
).
path
(
"/{id}"
).
build
(
product
.
getProductId
());
return
Response
.
created
(
uri
).
build
();
}
}
...
...
@@ -131,9 +135,10 @@ public class RProducts {
// POST używajmy wtedy, gdy nie wiemy z góry pod jakim adresem dane zostaną zapisane, np. gdy id rekordu jest generowane z sekwencji.
// Taka metoda powinna dać w odpowiedzi informację o ID utworzonego rekordu.
// Kilka możliwości:
// 1)
(w tej klasie) -
odesłanie uzupełnionego rekordu - trochę niewydajne, ale wygodne
// 1) odesłanie uzupełnionego rekordu - trochę niewydajne, ale wygodne
// 2) minimalny dokumencik JSON, w którego wnętrzu jest zawarta ta informacja (tak robi wiele usług w praktyce, np. PayU)
// 3) (zobaczymy jeszcze) - odesłać odpowiedź typu Created z nagłówkiem Location - najlepsze z punktu widzenia standardów/dobrych praktyk
// 3) (teraz w tej wersji) - odesłać odpowiedź typu Created z nagłówkiem Location - najlepsze z punktu widzenia standardów/dobrych praktyk
// Aby zwracać taie "techniczne" odpowiedzi, używa się klasy Response.
@DELETE
@Path
(
"/{id}"
)
...
...
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