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
75b3c1b0
Commit
75b3c1b0
authored
May 24, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwsze przykłady RestEasyClient
parent
9757ce16
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
217 additions
and
0 deletions
+217
-0
.gitignore
PC27-RestKlient/.gitignore
+1
-0
pom.xml
PC27-RestKlient/pom.xml
+5
-0
Klient11_RestClient.java
...lient/src/main/java/sklep/klient/Klient11_RestClient.java
+55
-0
Klient12_RestClient_String.java
...rc/main/java/sklep/klient/Klient12_RestClient_String.java
+35
-0
Klient13_RestClient_Multiformat.java
...in/java/sklep/klient/Klient13_RestClient_Multiformat.java
+58
-0
Klient14_RestClient_PDF.java
...t/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
+61
-0
wynik11.json
PC27-RestKlient/wynik11.json
+2
-0
No files found.
PC27-RestKlient/.gitignore
View file @
75b3c1b0
...
...
@@ -31,3 +31,4 @@ build/
### Mac OS ###
.DS_Store
/wynik03.json
PC27-RestKlient/pom.xml
View file @
75b3c1b0
...
...
@@ -28,5 +28,10 @@
<artifactId>
parsson
</artifactId>
<version>
1.1.7
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-client
</artifactId>
<version>
6.2.12.Final
</version>
</dependency>
</dependencies>
</project>
PC27-RestKlient/src/main/java/sklep/klient/Klient11_RestClient.java
0 → 100644
View file @
75b3c1b0
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.client.Invocation
;
import
jakarta.ws.rs.client.WebTarget
;
import
jakarta.ws.rs.core.Response
;
// Ten i kolejne przykłady pokazują jak aplikacja kliencka napisana w Javie może wysyłać
// zapytania do usługi REST-owej (głównie GET, jest też gdzieś POST)
// korzystając z technologii JAX-RS "po stronie klienta".
// Aby z tego skorzystać, do projektu trzeba dodać bibliotekę z implementacją JAX-RS.
// Tutaj jest to resteasy-client.
public
class
Klient11_RestClient
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy"
);
Client
client
=
ClientBuilder
.
newClient
();
System
.
out
.
println
(
"Przygotowuję zapytanie"
);
WebTarget
target
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
).
path
(
"products.json"
);
Invocation
invocation
=
target
.
request
().
buildGet
();
System
.
out
.
println
(
"Wysyłam zapytanie"
);
Response
response
=
invocation
.
invoke
();
// Wynikiem jest obiekt klasy Response - tej samej, co na serwerze (używaliśmy np. do generowania kodów 404).
// W obiekcie można sprawdzić informacji o odpowiedzi: media type, status code.
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
());
// Aby odczytać zawartość zwróconą przez serwer, używamy metody readEntity.
// (przy domyślnych ustawieniach) tę metodę można wywołać tylko raz.
// Dopiero w tym momencie podajemy typ, na który zostanie skonwertowana treść odpowiedzi
// (w miarę możliwości - po prostu niektóre typy zadziałają, a niektóre nie).
byte
[]
dane
=
response
.
readEntity
(
byte
[].
class
);
System
.
out
.
println
(
"Dane mają "
+
dane
.
length
+
" bajtów."
);
client
.
close
();
try
{
Files
.
write
(
Paths
.
get
(
"wynik11.json"
),
dane
);
System
.
out
.
println
(
"Zapisałem w pliku"
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
e
);
}
System
.
out
.
println
(
"Koniec"
);
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient12_RestClient_String.java
0 → 100644
View file @
75b3c1b0
package
sklep
.
klient
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.core.Response
;
public
class
Klient12_RestClient_String
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
())
{
// Taki styl programowania to "fluent API". Przy okazji - zarówno Client, jak i Response są "zamykalne.
try
(
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
());
// readEntity(OKREŚLENIE TYPU) stara się odczytać tresc odpowiedzi jako obiekt podanego typu
// Obsługiwane typy to m.in: byte[], String, InputStream, File
// Dodając odpowiednie "MessageBodyReader", możemy obsługiwać dowolne typy.
// W szczególności, gdy dodamy do projektu obsługę XML lub JSON (zob. zależności Mavena),
// będziemy mogli odczytywać dane w postaci obiektów naszego modelu, np. Product.
String
dane
=
response
.
readEntity
(
String
.
class
);
System
.
out
.
println
(
"Otrzymane dane:"
);
System
.
out
.
println
(
dane
);
}
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient13_RestClient_Multiformat.java
0 → 100644
View file @
75b3c1b0
package
sklep
.
klient
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
javax.swing.JOptionPane
;
import
jakarta.ws.rs.client.Client
;
import
jakarta.ws.rs.client.ClientBuilder
;
import
jakarta.ws.rs.core.Response
;
public
class
Klient13_RestClient_Multiformat
{
public
static
void
main
(
String
[]
args
)
{
try
(
Client
client
=
ClientBuilder
.
newClient
())
{
String
[]
formaty
=
{
"txt"
,
"json"
,
"xml"
,
"html"
,
"pdf"
};
String
format
=
(
String
)
JOptionPane
.
showInputDialog
(
null
,
"Wybierz format danych"
,
"Wybór"
,
JOptionPane
.
QUESTION_MESSAGE
,
null
,
formaty
,
"txt"
);
if
(
format
==
null
)
{
return
;
}
String
mediaType
=
switch
(
format
)
{
case
"txt"
->
"text/plain"
;
case
"json"
->
"application/json"
;
case
"xml"
->
"application/xml"
;
case
"html"
->
"text/html"
;
case
"pdf"
->
"application/pdf"
;
default
->
throw
new
IllegalArgumentException
();
};
// Klient może wybrać format (mediaType), w jakim oczekuje odpowiedzi - to wpływa na nagłówek Accept
try
(
Response
response
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
)
.
path
(
"products"
)
.
request
(
mediaType
)
.
buildGet
()
.
invoke
())
{
JOptionPane
.
showMessageDialog
(
null
,
String
.
format
(
"""
Status: %d
C-Type: %s
Length: %d"""
,
response
.
getStatus
(),
response
.
getMediaType
(),
response
.
getLength
()));
Path
plik
=
Paths
.
get
(
"wynik13."
+
format
);
InputStream
stream
=
response
.
readEntity
(
InputStream
.
class
);
Files
.
copy
(
stream
,
plik
,
StandardCopyOption
.
REPLACE_EXISTING
);
JOptionPane
.
showMessageDialog
(
null
,
"Zapisano plik "
+
plik
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-RestKlient/src/main/java/sklep/klient/Klient14_RestClient_PDF.java
0 → 100644
View file @
75b3c1b0
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..."
);
try
(
Client
client
=
ClientBuilder
.
newClient
())
{
WebTarget
root
=
client
.
target
(
Ustawienia
.
ADRES_USLUGI
);
try
(
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"
);
}
}
PC27-RestKlient/wynik11.json
0 → 100644
View file @
75b3c1b0
[{
"description"
:
"Pralka szybkoobrotowa"
,
"price"
:
2709.00
,
"productId"
:
1
,
"productName"
:
"praleczka"
,
"vat"
:
0.23
},{
"description"
:
"Odkurzacz automatyczny"
,
"price"
:
800.00
,
"productId"
:
2
,
"productName"
:
"odkurzacz"
,
"vat"
:
0.23
},{
"description"
:
"Telewizor 55 cali 4K"
,
"price"
:
3456.00
,
"productId"
:
3
,
"productName"
:
"telewizor 55
\"
"
,
"vat"
:
0.23
},{
"description"
:
"Telewizor 40 Full HD"
,
"price"
:
2200.00
,
"productId"
:
4
,
"productName"
:
"telewizor 40
\"
"
,
"vat"
:
0.23
},{
"price"
:
444.00
,
"productId"
:
5
,
"productName"
:
"myszka gejmerska"
,
"vat"
:
0.23
},{
"description"
:
"kawa z ekspresu"
,
"price"
:
15.50
,
"productId"
:
10
,
"productName"
:
"kawa"
,
"vat"
:
0.08
},{
"description"
:
"Herbata zielona"
,
"price"
:
23.00
,
"productId"
:
11
,
"productName"
:
"herbata"
,
"vat"
:
0.23
},{
"price"
:
2123.99
,
"productId"
:
12
,
"productName"
:
"pralka inna"
,
"vat"
:
0.23
},{
"description"
:
"Pralka wolnoobrotowa"
,
"price"
:
2123.99
,
"productId"
:
13
,
"productName"
:
"pralka inna"
,
"vat"
:
0.23
},{
"description"
:
"opis 1"
,
"price"
:
2000.00
,
"productId"
:
14
,
"productName"
:
"nowy 1"
,
"vat"
:
0.23
},{
"description"
:
"opis 2"
,
"price"
:
2000.00
,
"productId"
:
15
,
"productName"
:
"nowy 2"
,
"vat"
:
0.23
},{
"description"
:
"opis 3"
,
"price"
:
2000.00
,
"productId"
:
16
,
"productName"
:
"nowy 3"
,
"vat"
:
0.23
}]
\ No newline at end of file
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