Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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_20230928
Commits
b844947f
Commit
b844947f
authored
Nov 10, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moduł klient-rest
parent
9be2e02b
Show whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
680 additions
and
0 deletions
+680
-0
pom.xml
Wielomodulowy/pom.xml
+1
-0
.gitignore
Wielomodulowy/wielomodulowy-klient_rest/.gitignore
+1
-0
pom.xml
Wielomodulowy/wielomodulowy-klient_rest/pom.xml
+44
-0
Klient01_URL.java
...y-klient_rest/src/main/java/rest_klient/Klient01_URL.java
+25
-0
Klient02_URL_JSON.java
...ent_rest/src/main/java/rest_klient/Klient02_URL_JSON.java
+33
-0
Klient03_HttpClient.java
...t_rest/src/main/java/rest_klient/Klient03_HttpClient.java
+36
-0
Klient04_HttpClient_String.java
...src/main/java/rest_klient/Klient04_HttpClient_String.java
+29
-0
Klient05_HttpClient_Accept.java
...src/main/java/rest_klient/Klient05_HttpClient_Accept.java
+31
-0
Klient06_HttpClient_JSON.java
...t/src/main/java/rest_klient/Klient06_HttpClient_JSON.java
+49
-0
Klient07_HttpClient_JSON_Lista.java
...main/java/rest_klient/Klient07_HttpClient_JSON_Lista.java
+53
-0
Klient11_RestClient.java
...t_rest/src/main/java/rest_klient/Klient11_RestClient.java
+54
-0
Klient12_RestClient_String.java
...src/main/java/rest_klient/Klient12_RestClient_String.java
+34
-0
Klient13_RestClient_Multiformat.java
...ain/java/rest_klient/Klient13_RestClient_Multiformat.java
+59
-0
Klient14_RestClient_PDF.java
...st/src/main/java/rest_klient/Klient14_RestClient_PDF.java
+60
-0
Klient21_RestClient_JAXB.java
...t/src/main/java/rest_klient/Klient21_RestClient_JAXB.java
+33
-0
Klient22_RestClient_JSON.java
...t/src/main/java/rest_klient/Klient22_RestClient_JSON.java
+41
-0
Klient23_RestClient_JSON_JedenProdukt.java
...va/rest_klient/Klient23_RestClient_JSON_JedenProdukt.java
+31
-0
Klient24_Interaktywna_Edycja.java
...c/main/java/rest_klient/Klient24_Interaktywna_Edycja.java
+61
-0
Ustawienia.java
...owy-klient_rest/src/main/java/rest_klient/Ustawienia.java
+5
-0
No files found.
Wielomodulowy/pom.xml
View file @
b844947f
...
@@ -17,5 +17,6 @@
...
@@ -17,5 +17,6 @@
<module>
wielomodulowy-model
</module>
<module>
wielomodulowy-model
</module>
<module>
wielomodulowy-baza
</module>
<module>
wielomodulowy-baza
</module>
<module>
wielomodulowy-rest
</module>
<module>
wielomodulowy-rest
</module>
<module>
wielomodulowy-klient_rest
</module>
</modules>
</modules>
</project>
</project>
Wielomodulowy/wielomodulowy-klient_rest/.gitignore
0 → 100644
View file @
b844947f
/wynik*.*
Wielomodulowy/wielomodulowy-klient_rest/pom.xml
0 → 100644
View file @
b844947f
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
pl.alx.kjava
</groupId>
<artifactId>
Wielomodulowy
</artifactId>
<version>
1.0
</version>
</parent>
<artifactId>
wielomodulowy-klient_rest
</artifactId>
<properties>
<resteasy.version>
6.2.6.Final
</resteasy.version>
</properties>
<dependencies>
<dependency>
<groupId>
${project.groupId}
</groupId>
<artifactId>
wielomodulowy-model
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.eclipse.parsson
</groupId>
<artifactId>
parsson
</artifactId>
<version>
1.1.4
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-client
</artifactId>
<version>
${resteasy.version}
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-jaxb-provider
</artifactId>
<version>
${resteasy.version}
</version>
</dependency>
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-jackson2-provider
</artifactId>
<version>
${resteasy.version}
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient01_URL.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
public
class
Klient01_URL
{
public
static
void
main
(
String
[]
args
)
{
// Najprostszy sposób w Javie, aby pobrać dane z adresu URL, to użyć klasy URL.
try
{
URL
url
=
new
URL
(
"http://localhost:8080/PC35-RestSerwer/products.json"
);
System
.
out
.
println
(
"Odczytuję dane..."
);
try
(
InputStream
input
=
url
.
openStream
())
{
// teraz z inputa możemy czytać ciąg bajtów
// ja przerzucę bajty czytane z sieci bezpośrednio do System.out
// (w przypadku obcego serwisu może być to niebezpieczne)
input
.
transferTo
(
System
.
out
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient02_URL_JSON.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient02_URL_JSON
{
public
static
void
main
(
String
[]
args
)
{
try
{
URL
url
=
new
URL
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
try
(
InputStream
inputStream
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
inputStream
))
{
JsonArray
array
=
reader
.
readArray
();
// System.out.println(array);
for
(
JsonValue
jsonValue
:
array
)
{
//System.out.println(jsonValue);
System
.
out
.
println
(
jsonValue
.
asJsonObject
().
getString
(
"productName"
));
System
.
out
.
println
(
" opis: "
+
jsonValue
.
asJsonObject
().
getString
(
"description"
,
""
));
System
.
out
.
println
(
" cena: "
+
jsonValue
.
asJsonObject
().
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient03_HttpClient.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
public
class
Klient03_HttpClient
{
/* W Java 11 pojawiło się rozwiązanie "HttpClient", które umożliwia komunikację HTTP z dużą kontrolą nad szczegółami.
* Wysyłając zapytanie, od razu trzeba podać odpowiedni "BodyHandler",
* który pozwoli nam odczytać treść odpowiedzi we właściwy dla nas sposób.
*
* W tej wersji wynik jest zapisywany do pliku.
*/
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
Path
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofFile
(
Paths
.
get
(
"wynik03.json"
)));
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"OK, zapisany plik: "
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient04_HttpClient_String.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
public
class
Klient04_HttpClient_String
{
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
// Body z odpowiedzi pobierzemy jako obiekt String z całą treścią
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
response
.
body
());
// tutaj
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient05_HttpClient_Accept.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
public
class
Klient05_HttpClient_Accept
{
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products"
);
// W tej wersji do zapytania dodajemy nagłówek Accept
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"text/plain"
)
.
build
();
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient06_HttpClient_JSON.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
public
class
Klient06_HttpClient_JSON
{
/* W tej wersji używamy technologii Jakarta JSON P.
*
* Widzimy drzewo danych jsonowych i jego elementytakie jak JsonObject, JsonArray, ...
* Mamy dostęp do poszczególnych pól.
*/
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products/1"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"application/json"
)
.
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
product
=
reader
.
readObject
();
System
.
out
.
println
(
"Pobrany obiekt jsonowy: "
+
product
);
String
nazwa
=
product
.
getString
(
"productName"
);
String
opis
=
product
.
getString
(
"description"
,
"BRAK OPISU"
);
BigDecimal
cena
=
product
.
getJsonNumber
(
"price"
).
bigDecimalValue
();
System
.
out
.
println
(
nazwa
+
" za cenę "
+
cena
+
" , opis: "
+
opis
);
reader
.
close
();
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient07_HttpClient_JSON_Lista.java
0 → 100644
View file @
b844947f
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient07_HttpClient_JSON_Lista
{
public
static
void
main
(
String
[]
args
)
{
try
{
JsonArray
array
=
pobierzJsona
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
for
(
JsonValue
jsonValue
:
array
)
{
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
if
(
jsonObject
.
containsKey
(
"description"
))
{
System
.
out
.
println
(
" opis: "
+
jsonObject
.
getString
(
"description"
,
""
));
}
System
.
out
.
println
(
" cena: "
+
jsonObject
.
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
catch
(
IOException
|
InterruptedException
|
URISyntaxException
e
)
{
e
.
printStackTrace
();
}
}
private
static
JsonArray
pobierzJsona
(
String
adres
)
throws
IOException
,
InterruptedException
,
URISyntaxException
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
return
wczytajJsona
(
response
.
body
());
}
private
static
JsonArray
wczytajJsona
(
InputStream
input
)
{
try
(
JsonReader
reader
=
Json
.
createReader
(
input
))
{
return
reader
.
readArray
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient11_RestClient.java
0 → 100644
View file @
b844947f
package
rest_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."
);
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"
);
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient12_RestClient_String.java
0 → 100644
View file @
b844947f
package
rest_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
)
{
Client
client
=
ClientBuilder
.
newClient
();
// Taki styl programowania to "fluent API"
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 "MeassgeBodyReader", 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
);
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient13_RestClient_Multiformat.java
0 → 100644
View file @
b844947f
package
rest_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
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
();
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient14_RestClient_PDF.java
0 → 100644
View file @
b844947f
package
rest_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"
);
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient21_RestClient_JAXB.java
0 → 100644
View file @
b844947f
package
rest_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
);
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient22_RestClient_JSON.java
0 → 100644
View file @
b844947f
package
rest_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
);
// albo jednolinijkowo:
// List<Product> products = response.readEntity(new GenericType<List<Product>>() {});
for
(
Product
product
:
products
)
{
System
.
out
.
println
(
product
);
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient23_RestClient_JSON_JedenProdukt.java
0 → 100644
View file @
b844947f
package
rest_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
);
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Klient24_Interaktywna_Edycja.java
0 → 100644
View file @
b844947f
package
rest_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..."
);
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ć"
);
}
}
}
}
Wielomodulowy/wielomodulowy-klient_rest/src/main/java/rest_klient/Ustawienia.java
0 → 100644
View file @
b844947f
package
rest_klient
;
public
class
Ustawienia
{
public
static
final
String
ADRES_USLUGI
=
"http://localhost:8080/wielomodulowy-rest"
;
}
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