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
f52b6c86
Commit
f52b6c86
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wiele formatów w @Consumes i @Produces
parent
64ab8675
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
11 deletions
+12
-11
RProduct.java
PC35-RestSerwer/src/main/java/rest/RProduct.java
+12
-11
No files found.
PC35-RestSerwer/src/main/java/rest/RProduct.java
View file @
f52b6c86
package
rest
;
package
rest
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.DELETE
;
...
@@ -17,6 +16,7 @@ import sklep.db.DBException;
...
@@ -17,6 +16,7 @@ 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
;
import
sklep.photo.PhotoUtil
;
import
sklep.photo.PhotoUtil
;
// JAX-RS - część Javy EE
// JAX-RS - część Javy EE
...
@@ -27,19 +27,20 @@ import sklep.photo.PhotoUtil;
...
@@ -27,19 +27,20 @@ import sklep.photo.PhotoUtil;
@Path
(
"/products"
)
@Path
(
"/products"
)
public
class
RProduct
{
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
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
,
"text/plain"
}
)
public
List
<
Product
>
readAllProducts
()
throws
DBException
{
public
ProductList
readAllProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
(
);
return
new
ProductList
(
productDAO
.
readAll
()
);
}
}
}
}
@Path
(
"/{id}"
)
@Path
(
"/{id}"
)
@GET
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
,
"text/plain"
}
)
public
Product
readOneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
public
Product
readOneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
...
@@ -56,7 +57,7 @@ public class RProduct {
...
@@ -56,7 +57,7 @@ public class RProduct {
@Path
(
"/{id}/price"
)
@Path
(
"/{id}/price"
)
@GET
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"text/plain"
}
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
...
@@ -71,7 +72,7 @@ public class RProduct {
...
@@ -71,7 +72,7 @@ public class RProduct {
// Właśnie poprzez ten parametr przekazywane są dane przysłane w treści zapytania (w praktyce: POST i PUT).
// Właśnie poprzez ten parametr przekazywane są dane przysłane w treści zapytania (w praktyce: POST i PUT).
@Path
(
"/{id}/price"
)
@Path
(
"/{id}/price"
)
@PUT
@PUT
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"text/plain"
}
)
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
...
@@ -84,7 +85,7 @@ public class RProduct {
...
@@ -84,7 +85,7 @@ public class RProduct {
@Path
(
"/{id}"
)
@Path
(
"/{id}"
)
@PUT
@PUT
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"application/xml"
}
)
public
void
updateProduct
(
@PathParam
(
"id"
)
int
productId
,
Product
product
)
throws
DBException
{
public
void
updateProduct
(
@PathParam
(
"id"
)
int
productId
,
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
...
@@ -105,8 +106,8 @@ public class RProduct {
...
@@ -105,8 +106,8 @@ public class RProduct {
// a aplikacja sama wybierze nowe ID z sekwencjii
// a aplikacja sama wybierze nowe ID z sekwencjii
// w wyniku zostanie odesłany uzupełniony produkt
// w wyniku zostanie odesłany uzupełniony produkt
@POST
@POST
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"application/xml"
}
)
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
}
)
public
Product
addProduct
(
Product
product
)
throws
DBException
{
public
Product
addProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
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