Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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
alx_java2b_20250412
Commits
6a22e796
Commit
6a22e796
authored
May 11, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa różnych formatów w REST
parent
08f17d86
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
115 additions
and
11 deletions
+115
-11
Product.java
PC24-SklepSoap/src/main/java/sklep/model/Product.java
+3
-0
Product.java
PC26-RestSerwer/src/main/java/sklep/model/Product.java
+5
-0
PhotoUtil.java
PC26-RestSerwer/src/main/java/sklep/photo/PhotoUtil.java
+1
-1
RProduct.java
PC26-RestSerwer/src/main/java/sklep/rest/RProduct.java
+11
-10
RProductJSON.java
PC26-RestSerwer/src/main/java/sklep/rest/RProductJSON.java
+95
-0
No files found.
PC24-SklepSoap/src/main/java/sklep/model/Product.java
View file @
6a22e796
...
...
@@ -62,6 +62,9 @@ public class Product {
return
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
@Override
public
int
hashCode
()
{
...
...
PC26-RestSerwer/src/main/java/sklep/model/Product.java
View file @
6a22e796
...
...
@@ -5,7 +5,9 @@ import java.util.Objects;
import
jakarta.xml.bind.annotation.XmlAttribute
;
import
jakarta.xml.bind.annotation.XmlElement
;
import
jakarta.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
public
class
Product
{
@XmlAttribute
(
name
=
"id"
)
private
Integer
productId
;
...
...
@@ -62,6 +64,9 @@ public class Product {
return
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
@Override
public
int
hashCode
()
{
...
...
PC26-RestSerwer/src/main/java/sklep/photo/PhotoUtil.java
View file @
6a22e796
...
...
@@ -53,10 +53,10 @@ public class PhotoUtil {
e
.
printStackTrace
();
}
}
private
static
Path
getPath
(
int
productId
)
throws
DBException
{
String
dir
=
DBSettings
.
load
().
getProperty
(
"photo_dir"
);
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
}
PC26-RestSerwer/src/main/java/sklep/rest/RProduct.java
View file @
6a22e796
...
...
@@ -21,7 +21,7 @@ import sklep.photo.PhotoUtil;
@Path
(
"/products"
)
public
class
RProduct
{
@GET
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
,
"text/plain"
}
)
public
List
<
Product
>
readAllProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -31,7 +31,7 @@ public class RProduct {
@GET
@Path
(
"/{id}"
)
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"application/xml"
,
"text/plain"
}
)
public
Product
readOneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -45,6 +45,7 @@ public class RProduct {
// czyli nie wiemy, pod adresem zapisze się nowy produkt.
// POST potrafi "dodać rekord do katalogu".
@POST
@Consumes
({
"application/json"
,
"application/xml"
})
public
void
saveProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -57,7 +58,7 @@ public class RProduct {
// do poszczególnych pól obiektu (aby nie transferować całego rekordu, gdy potrzebna tylko jedna informacja)
@GET
@Path
(
"/{id}/price"
)
@Produces
(
"application/json"
)
@Produces
(
{
"application/json"
,
"text/plain"
}
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
readOneProduct
(
productId
).
getPrice
();
}
...
...
@@ -105,16 +106,16 @@ public class RProduct {
@GET
@Path
(
"/{id}/photo"
)
// przykładowo /
api/
products/3/photo
// przykładowo /products/3/photo
@Produces
(
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
PhotoUtil
.
readBytes
(
productId
);
}
//
@PUT
//
@Path("/{id}/photo")
//
@Consumes("image/jpeg")
//
public void writePhoto(@PathParam("id") int productId, byte[] bajty) {
//
PhotoUtil.writeBytes(productId, bajty);
//
}
@PUT
@Path
(
"/{id}/photo"
)
@Consumes
(
"image/jpeg"
)
public
void
writePhoto
(
@PathParam
(
"id"
)
int
productId
,
byte
[]
bajty
)
{
PhotoUtil
.
writeBytes
(
productId
,
bajty
);
}
}
PC26-RestSerwer/src/main/java/sklep/rest/RProductJSON.java
0 → 100644
View file @
6a22e796
package
sklep
.
rest
;
import
java.math.BigDecimal
;
import
java.util.List
;
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
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
@Path
(
"/products.json"
)
// na poziomie klasy można podać domyślne wartości Produces i Consumes dla wszystkich metod
@Produces
(
"application/json"
)
@Consumes
(
"application/json"
)
public
class
RProductJSON
{
@GET
public
List
<
Product
>
readAllProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
();
}
}
@GET
@Path
(
"/{id}"
)
public
Product
readOneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
findById
(
productId
);
}
}
@POST
public
void
saveProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
}
}
@GET
@Path
(
"/{id}/price"
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
readOneProduct
(
productId
).
getPrice
();
}
@PUT
@Path
(
"/{id}/price"
)
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
product
.
setPrice
(
newPrice
);
productDAO
.
save
(
product
);
db
.
commit
();
}
}
@DELETE
@Path
(
"/{id}"
)
public
void
usun
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
delete
(
productId
);
db
.
commit
();
}
}
// Jeśli na poziomie metody użyjemy adnotacji @Produces lub @Consumes,
// to ustawienie metody nadpisuje ogólne ustawienie dla klasy
@GET
@Path
(
"/{id}/photo"
)
@Produces
(
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
PhotoUtil
.
readBytes
(
productId
);
}
@PUT
@Path
(
"/{id}/photo"
)
@Consumes
(
"image/jpeg"
)
public
void
writePhoto
(
@PathParam
(
"id"
)
int
productId
,
byte
[]
bajty
)
{
PhotoUtil
.
writeBytes
(
productId
,
bajty
);
}
}
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