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
83a46550
Commit
83a46550
authored
Aug 20, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady RestClient wykorzystujące klasy modelu
parent
9e5f77c5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
224 additions
and
0 deletions
+224
-0
Klient14_RestClient_PDF.java
...t/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
+60
-0
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
+39
-0
Klient23_RestClient_JSON_JedenProdukt.java
...a/sklep/klient/Klient23_RestClient_JSON_JedenProdukt.java
+31
-0
Klient24_Interaktywny.java
...ent/src/main/java/sklep/klient/Klient24_Interaktywny.java
+61
-0
No files found.
PC33-RestKlient/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
0 → 100644
View file @
83a46550
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
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
;
public
class
Klient14_RestClient_PDF
{
private
static
final
MediaType
PDF_TYPE
=
new
MediaType
(
"application"
,
"pdf"
);
public
static
void
main
(
String
[]
args
)
{
int
productId
=
1
;
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
root
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
);
Response
response
=
root
.
path
(
"products"
)
.
path
(
"{id}"
)
.
resolveTemplate
(
"id"
,
productId
)
.
request
()
.
accept
(
PDF_TYPE
)
.
buildGet
()
.
invoke
();
System
.
out
.
println
(
"Otrzymałem response: "
+
response
);
System
.
out
.
println
(
"Status: "
+
response
.
getStatus
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
getMediaType
());
if
(
response
.
getStatus
()
!=
200
)
{
System
.
out
.
println
(
"Chyba coś nie tak, więc przerywam."
);
return
;
}
String
nazwaPliku
=
"wynik.pdf"
;
String
contentDisposition
=
response
.
getHeaderString
(
"Content-Disposition"
);
if
(
contentDisposition
!=
null
&&
contentDisposition
.
contains
(
";filename="
))
{
nazwaPliku
=
contentDisposition
.
split
(
";filename="
)[
1
];
}
try
(
InputStream
strumienDanych
=
response
.
readEntity
(
InputStream
.
class
))
{
long
ileBajtow
=
Files
.
copy
(
strumienDanych
,
Paths
.
get
(
nazwaPliku
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
printf
(
"Zapisano %d bajtów do pliku %s\n"
,
ileBajtow
,
nazwaPliku
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"Gotowe"
);
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient21_RestClient_JAXB.java
0 → 100644
View file @
83a46550
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
)
{
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
);
}
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient22_RestClient_JSON.java
0 → 100644
View file @
83a46550
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
)
{
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
);
for
(
Product
product
:
products
)
{
System
.
out
.
println
(
product
);
}
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient23_RestClient_JSON_JedenProdukt.java
0 → 100644
View file @
83a46550
package
sklep
.
klient
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
sklep.model.Product
;
public
class
Klient23_RestClient_JSON_JedenProdukt
{
public
static
void
main
(
String
[]
args
)
{
Client
client
=
ClientBuilder
.
newClient
();
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products"
)
.
path
(
"1"
)
.
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
);
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient24_Interaktywny.java
0 → 100644
View file @
83a46550
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_Interaktywny
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
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
;
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..."
);
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ć"
);
}
}
}
}
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