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
f8ef5b6d
Commit
f8ef5b6d
authored
May 24, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Klient obiektowy
parent
02bb20c1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
212 additions
and
3 deletions
+212
-3
.gitignore
PC27-RestKlient/.gitignore
+3
-0
pom.xml
PC27-RestKlient/pom.xml
+12
-1
Klient12_RestClient_String.java
...rc/main/java/sklep/klient/Klient12_RestClient_String.java
+1
-1
Klient14_RestClient_PDF.java
...t/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
+2
-1
Klient21_RestClient_JAXB.java
.../src/main/java/sklep/klient/Klient21_RestClient_JAXB.java
+33
-0
Klient22_RestClient_JSON.java
.../src/main/java/sklep/klient/Klient22_RestClient_JSON.java
+42
-0
Klient23_RestClient_JSON_JedenProdukt.java
...a/sklep/klient/Klient23_RestClient_JSON_JedenProdukt.java
+47
-0
Klient24_Interaktywna_Edycja.java
.../main/java/sklep/klient/Klient24_Interaktywna_Edycja.java
+64
-0
pobierz_produkty.py
...RestKlient/src/main/java/sklep/klient/pobierz_produkty.py
+8
-0
No files found.
PC27-RestKlient/.gitignore
View file @
f8ef5b6d
...
@@ -32,3 +32,6 @@ build/
...
@@ -32,3 +32,6 @@ build/
### Mac OS ###
### Mac OS ###
.DS_Store
.DS_Store
/wynik03.json
/wynik03.json
/wynik11.json
/product0001.pdf
/wynik13.txt
PC27-RestKlient/pom.xml
View file @
f8ef5b6d
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
<maven.compiler.source>
21
</maven.compiler.source>
<maven.compiler.source>
21
</maven.compiler.source>
<maven.compiler.target>
21
</maven.compiler.target>
<maven.compiler.target>
21
</maven.compiler.target>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<resteasy.version>
6.2.12.Final
</resteasy.version>
</properties>
</properties>
<dependencies>
<dependencies>
...
@@ -31,7 +32,17 @@
...
@@ -31,7 +32,17 @@
<dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-client
</artifactId>
<artifactId>
resteasy-client
</artifactId>
<version>
6.2.12.Final
</version>
<version>
${resteasy.version}
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-jackson2-provider
</artifactId>
<version>
${resteasy.version}
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-jaxb-provider
</artifactId>
<version>
${resteasy.version}
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
PC27-RestKlient/src/main/java/sklep/klient/Klient12_RestClient_String.java
View file @
f8ef5b6d
...
@@ -8,7 +8,7 @@ public class Klient12_RestClient_String {
...
@@ -8,7 +8,7 @@ public class Klient12_RestClient_String {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
())
{
try
(
Client
client
=
ClientBuilder
.
newClient
())
{
// Taki styl programowania to "fluent API". Przy okazji - zarówno Client, jak i Response są "zamykalne.
// Taki styl programowania to "fluent API". Przy okazji - zarówno Client, jak i Response są "zamykalne
"
.
try
(
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
try
(
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products.json"
)
.
path
(
"products.json"
)
.
request
()
.
request
()
...
...
PC27-RestKlient/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
View file @
f8ef5b6d
...
@@ -24,7 +24,8 @@ public class Klient14_RestClient_PDF {
...
@@ -24,7 +24,8 @@ public class Klient14_RestClient_PDF {
WebTarget
root
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
);
WebTarget
root
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
);
try
(
Response
response
=
root
try
(
Response
response
=
root
.
path
(
"products"
)
.
path
(
"products.pdf"
)
//.path("products")
.
path
(
"{id}"
)
.
path
(
"{id}"
)
.
resolveTemplate
(
"id"
,
productId
)
.
resolveTemplate
(
"id"
,
productId
)
.
request
()
.
request
()
...
...
PC27-RestKlient/src/main/java/sklep/klient/Klient21_RestClient_JAXB.java
0 → 100644
View file @
f8ef5b6d
package
sklep
.
klient
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.core.Response
;
import
sklep.model.Product
;
import
sklep.model.ProductList
;
public
class
Klient21_RestClient_JAXB
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
();
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products.xml"
)
.
request
()
.
buildGet
()
.
invoke
())
{
System
.
out
.
println
(
"Mam odpowiedź: "
+
response
);
System
.
out
.
println
(
"Status: "
+
response
.
getStatus
());
System
.
out
.
println
(
"C-Type: "
+
response
.
getMediaType
());
System
.
out
.
println
(
"Length: "
+
response
.
getLength
());
ProductList
products
=
response
.
readEntity
(
ProductList
.
class
);
System
.
out
.
println
(
"Otrzymane dane:"
);
for
(
Product
product
:
products
.
getProducts
())
{
System
.
out
.
println
(
product
);
}
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient22_RestClient_JSON.java
0 → 100644
View file @
f8ef5b6d
package
sklep
.
klient
;
import
java.util.List
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.core.GenericType
;
import
jakarta.ws.rs.core.Response
;
import
sklep.model.Product
;
public
class
Klient22_RestClient_JSON
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
();
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products.json"
)
.
request
()
.
buildGet
()
.
invoke
())
{
System
.
out
.
println
(
"Mam odpowiedź: "
+
response
);
System
.
out
.
println
(
"Status: "
+
response
.
getStatus
());
System
.
out
.
println
(
"C-Type: "
+
response
.
getMediaType
());
System
.
out
.
println
(
"Length: "
+
response
.
getLength
());
// Ponieważ wersja JSON na serwerze zwraca wynik typu List<Product>, to tutaj musimy podać "typ generyczny",
// a nie wystarczy zwykła klasa.
// Nie zadziała:
// List<Product> products = response.readEntity(List.class);
GenericType
<
List
<
Product
>>
typListy
=
new
GenericType
<>()
{
};
List
<
Product
>
products
=
response
.
readEntity
(
typListy
);
// albo jednolinijkowo:
// List<Product> products = response.readEntity(new GenericType<List<Product>>() {});
for
(
Product
product
:
products
)
{
System
.
out
.
println
(
product
);
}
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient23_RestClient_JSON_JedenProdukt.java
0 → 100644
View file @
f8ef5b6d
package
sklep
.
klient
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.client.WebTarget
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
sklep.model.Product
;
import
java.util.Scanner
;
public
class
Klient23_RestClient_JSON_JedenProdukt
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
();
Scanner
scanner
=
new
Scanner
(
System
.
in
))
{
final
WebTarget
path
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products"
)
.
path
(
"{id}"
);
while
(
true
)
{
System
.
out
.
print
(
"Podaj id produktu (-1 kończy): "
);
int
id
=
Integer
.
parseInt
(
scanner
.
nextLine
());
if
(
id
==
-
1
)
{
break
;
}
try
(
Response
response
=
path
.
resolveTemplate
(
"id"
,
id
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_JSON
)
.
buildGet
()
.
invoke
())
{
System
.
out
.
println
(
"Mam odpowiedź: "
+
response
);
System
.
out
.
println
(
"Status: "
+
response
.
getStatus
());
System
.
out
.
println
(
"C-Type: "
+
response
.
getMediaType
());
System
.
out
.
println
(
"Length: "
+
response
.
getLength
());
Product
product
=
response
.
readEntity
(
Product
.
class
);
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
}
}
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient24_Interaktywna_Edycja.java
0 → 100644
View file @
f8ef5b6d
package
sklep
.
klient
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.client.Entity
;
import
jakarta.ws.rs.client.WebTarget
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
sklep.model.Product
;
public
class
Klient24_Interaktywna_Edycja
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
try
(
Scanner
scanner
=
new
Scanner
(
System
.
in
);
Client
client
=
ClientBuilder
.
newClient
())
{
WebTarget
path
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products"
)
.
path
(
"{id}"
);
System
.
out
.
println
(
"Przygotowana ścieżka: "
+
path
);
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id: "
);
int
id
=
scanner
.
nextInt
();
if
(
id
==
0
)
break
;
try
(
Response
response
=
path
.
resolveTemplate
(
"id"
,
id
)
.
request
(
MediaType
.
APPLICATION_JSON
)
.
get
())
{
System
.
out
.
println
(
"Status: "
+
response
.
getStatus
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
getMediaType
());
if
(
response
.
getStatus
()
==
200
)
{
Product
product
=
response
.
readEntity
(
Product
.
class
);
System
.
out
.
println
(
"Mam produkt:"
);
System
.
out
.
println
(
" Nazwa: "
+
product
.
getProductName
());
System
.
out
.
println
(
" Cena: "
+
product
.
getPrice
());
System
.
out
.
println
(
" Opis: "
+
product
.
getDescription
());
System
.
out
.
println
();
System
.
out
.
println
(
"Podaj zmianę ceny (0 aby nie zmieniać):"
);
BigDecimal
zmianaCeny
=
scanner
.
nextBigDecimal
();
if
(
zmianaCeny
.
compareTo
(
BigDecimal
.
ZERO
)
!=
0
)
{
BigDecimal
newPrice
=
product
.
getPrice
().
add
(
zmianaCeny
);
System
.
out
.
println
(
"PUT nowej ceny..."
);
try
(
Response
odpPut
=
path
.
path
(
"price"
).
resolveTemplate
(
"id"
,
id
).
request
()
.
put
(
Entity
.
entity
(
newPrice
,
MediaType
.
TEXT_PLAIN_TYPE
)))
{
System
.
out
.
println
(
"PUT zakończył się kodem "
+
odpPut
.
getStatus
());
}
}
}
else
{
System
.
out
.
println
(
"nie mogę odczytać"
);
}
}
}
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/pobierz_produkty.py
0 → 100644
View file @
f8ef5b6d
import
requests
dane
=
requests
.
get
(
"http://localhost:8080/PC27-RestSerwer/api/products.json"
)
.
json
()
print
(
"Liczba elementów"
,
len
(
dane
))
for
produkt
in
dane
:
print
(
f
'Produckt o nazwie {produkt["productName"]} ma cenę {produkt["price"]}'
)
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