Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
2fdee964
Commit
2fdee964
authored
May 25, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe klasy zasobów
parent
115a0b2b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
235 additions
and
0 deletions
+235
-0
ProductEndpoint.java
...-RestSerwer/src/main/java/sklep/rest/ProductEndpoint.java
+70
-0
RCustomer.java
PC33-RestSerwer/src/main/java/sklep/rest/RCustomer.java
+78
-0
ROrder.java
PC33-RestSerwer/src/main/java/sklep/rest/ROrder.java
+87
-0
No files found.
PC33-RestSerwer/src/main/java/sklep/rest/ProductEndpoint.java
0 → 100644
View file @
2fdee964
package
sklep
.
rest
;
import
java.util.List
;
import
jakarta.enterprise.context.RequestScoped
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.DELETE
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.POST
;
import
jakarta.ws.rs.PUT
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.QueryParam
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.Response.Status
;
import
sklep.model.Product
;
// Tak wygląda klasa wygenerowana przez polecenie "New JAX-RS Resource" w Eclipse z dodatkiem wtyczki JBossTolls (JAX-RS Tools)
// Komentuję adnotacje przed klasą, aby nie wpływało to na działanie aplikacji.
// @RequestScoped
// @Path("/products")
// @Produces({ "application/xml", "application/json" })
// @Consumes({ "application/xml", "application/json" })
public
class
ProductEndpoint
{
@POST
public
Response
create
(
final
Product
product
)
{
//TODO: process the given product
//you may want to use the following return statement, assuming that Product#getId() or a similar method
//would provide the identifier to retrieve the created Product resource:
//return Response.created(UriBuilder.fromResource(ProductEndpoint.class).path(String.valueOf(product.getId())).build()).build();
return
Response
.
created
(
null
).
build
();
}
@GET
@Path
(
"/{id:[0-9][0-9]*}"
)
public
Response
findById
(
@PathParam
(
"id"
)
final
Long
id
)
{
//TODO: retrieve the product
Product
product
=
null
;
if
(
product
==
null
)
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
return
Response
.
ok
(
product
).
build
();
}
@GET
public
List
<
Product
>
listAll
(
@QueryParam
(
"start"
)
final
Integer
startPosition
,
@QueryParam
(
"max"
)
final
Integer
maxResult
)
{
//TODO: retrieve the products
final
List
<
Product
>
products
=
null
;
return
products
;
}
@PUT
@Path
(
"/{id:[0-9][0-9]*}"
)
public
Response
update
(
@PathParam
(
"id"
)
Long
id
,
final
Product
product
)
{
//TODO: process the given product
return
Response
.
noContent
().
build
();
}
@DELETE
@Path
(
"/{id:[0-9][0-9]*}"
)
public
Response
deleteById
(
@PathParam
(
"id"
)
final
Long
id
)
{
//TODO: process the product matching by the given id
return
Response
.
noContent
().
build
();
}
}
PC33-RestSerwer/src/main/java/sklep/rest/RCustomer.java
0 → 100644
View file @
2fdee964
package
sklep
.
rest
;
import
java.util.List
;
import
jakarta.enterprise.context.RequestScoped
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.DELETE
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.POST
;
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.CustomerDAO
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Customer
;
@RequestScoped
@Path
(
"/customers"
)
@Produces
({
"application/xml"
,
"application/json"
})
@Consumes
({
"application/xml"
,
"application/json"
})
public
class
RCustomer
{
@POST
public
Response
save
(
final
Customer
customer
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customerDAO
.
save
(
customer
);
db
.
commit
();
return
Response
.
created
(
UriBuilder
.
fromResource
(
RCustomer
.
class
).
path
(
String
.
valueOf
(
customer
.
getEmail
())).
build
()).
build
();
}
}
@GET
@Path
(
"/{id}"
)
public
Customer
findById
(
@PathParam
(
"id"
)
final
String
email
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
return
customerDAO
.
findByEmail
(
email
);
}
}
@GET
public
List
<
Customer
>
listAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
return
customerDAO
.
readAll
();
}
}
@PUT
@Path
(
"/{id}"
)
public
Response
update
(
@PathParam
(
"id"
)
String
email
,
final
Customer
customer
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customer
.
setEmail
(
email
);
customerDAO
.
save
(
customer
);
db
.
commit
();
}
return
Response
.
noContent
().
build
();
}
@DELETE
@Path
(
"/{id}"
)
public
Response
deleteById
(
@PathParam
(
"id"
)
String
email
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customerDAO
.
delete
(
email
);
db
.
commit
();
}
return
Response
.
noContent
().
build
();
}
}
PC33-RestSerwer/src/main/java/sklep/rest/ROrder.java
0 → 100644
View file @
2fdee964
package
sklep
.
rest
;
import
java.net.URI
;
import
java.util.List
;
import
jakarta.enterprise.context.RequestScoped
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.GET
;
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.Response.Status
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.OrderDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Order
;
@RequestScoped
@Path
(
"/orders"
)
@Produces
({
"application/xml"
,
"application/json"
})
@Consumes
({
"application/xml"
,
"application/json"
})
public
class
ROrder
{
@GET
public
List
<
Order
>
listAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
return
orderDAO
.
readAll
();
}
}
@GET
@Path
(
"/{id:[0-9][0-9]*}"
)
public
Response
findById
(
@PathParam
(
"id"
)
final
Integer
id
)
{
// Klasa Response pozwala nam z pełną precyzją przygotować odpowiedź, która ma zostać odesłana klientowi.
// W przypadku pozytywnym (ok) zostanie odesłany obiekt przetłumaczony na XML lub JSON, a kod wynikowy to będzie 200.
// Ale w przypadku błędów możemy sami zdecydować co odsyłami (tutaj odpowiednie kody HTTP).
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
Order
order
=
orderDAO
.
findById
(
id
);
return
Response
.
ok
(
order
).
build
();
}
catch
(
DBException
e
)
{
e
.
printStackTrace
();
return
Response
.
status
(
Status
.
INTERNAL_SERVER_ERROR
).
build
();
}
catch
(
RecordNotFound
e
)
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
}
/*
// Metoda, która ma obsłużyć pobranie info o właścicielu zamówienia:
// /orders/1/customer
// W tej wersji metoda zwraca bezpośrednio dane klienta.
// Wada tego podejścia: ten sam rekord (konkretny klient) jest widoczny pod różnymi adresami URL.
@GET
@Path("/{id:[0-9][0-9]*}/customer")
public Customer getCustomer(@PathParam("id") Integer orderId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
CustomerDAO customerDAO = db.customerDAO();
Order order = orderDAO.findById(orderId);
Customer customer = customerDAO.findByEmail(order.getCustomerEmail());
return customer;
}
}
*/
// W tej wersji w odpowiedzi na zapytanie o dane klienta, który złożył zamówienie,
// wyślemy przekierowanie pod adres tego klienta.
// To jest lepsze z punktu widzenia "dobrych praktyk REST".
@GET
@Path
(
"/{id:[0-9][0-9]*}/customer"
)
public
Response
getCustomer
(
@PathParam
(
"id"
)
Integer
orderId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
Order
order
=
orderDAO
.
findById
(
orderId
);
URI
customerURI
=
UriBuilder
.
fromResource
(
RCustomer
.
class
)
.
path
(
"/{email}"
)
.
build
(
order
.
getCustomerEmail
());
return
Response
.
seeOther
(
customerURI
).
build
();
}
}
}
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