Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
4a6d3dda
Commit
4a6d3dda
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kolejne przykłady klienta REST
parent
300f2911
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
357 additions
and
1 deletions
+357
-1
Klient1_URL.java
PC36-RestKlient/src/main/java/sklep/klient/Klient1_URL.java
+29
-0
Klient2_InputStream.java
...lient/src/main/java/sklep/klient/Klient2_InputStream.java
+63
-0
Klient3_InputStream.java
...lient/src/main/java/sklep/klient/Klient3_InputStream.java
+47
-0
Klient4_InputStream_Accept.java
...rc/main/java/sklep/klient/Klient4_InputStream_Accept.java
+49
-0
Klient5_InputStream_AcceptPDF.java
...main/java/sklep/klient/Klient5_InputStream_AcceptPDF.java
+60
-0
Klient6_JsonTekstowo.java
...ient/src/main/java/sklep/klient/Klient6_JsonTekstowo.java
+40
-0
Klient7_RozneKlasy.java
...Klient/src/main/java/sklep/klient/Klient7_RozneKlasy.java
+48
-0
PrzykladoweZapytanie.java
...ient/src/main/java/sklep/klient/PrzykladoweZapytanie.java
+0
-1
Ustawienia.java
PC36-RestKlient/src/main/java/sklep/klient/Ustawienia.java
+21
-0
No files found.
PC36-RestKlient/src/main/java/sklep/klient/Klient1_URL.java
0 → 100644
View file @
4a6d3dda
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
public
class
Klient1_URL
{
// Najprostszy sposób w Javie, aby pobrać jakieś dane z sieci, to użyć klasy URL
public
static
void
main
(
String
[]
args
)
{
String
adres
=
Ustawienia
.
URL_SERWERA
+
"products/1"
;
try
{
System
.
out
.
println
(
"Otwieram adres..."
);
URL
url
=
new
URL
(
adres
);
try
(
InputStream
input
=
url
.
openStream
())
{
System
.
out
.
println
(
"Czytam dane..."
);
Files
.
copy
(
input
,
Paths
.
get
(
"produkt1.txt"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"Gotowe"
);
};
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient2_InputStream.java
0 → 100644
View file @
4a6d3dda
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
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.Invocation
;
import
javax.ws.rs.client.WebTarget
;
import
javax.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
Klient2_InputStream
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
System
.
out
.
println
(
"Przygotowuję zapytanie..."
);
WebTarget
target
=
client
.
target
(
Ustawienia
.
URL_SERWERA
+
"products"
);
Invocation
.
Builder
requestBuilder
=
target
.
request
();
Invocation
invocation
=
requestBuilder
.
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
(
"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
;
}
// Aby odczytać zawartość zwróconą przez serwer, uzywamy metody readEntity.
// (przy domyślnych ustawieniach) tę metodę można wywołać tylko raz.
// W tej wersji dane odbieramy w postaci "surowej" jako ciąg bajtów za pomocą InputStream.
// Jest to najbardziej wydajne, ale jednocześnie mało wygodne, podejście.
String
nazwaPliku
=
"wynik2."
+
Ustawienia
.
rozszerzenieDlaTypu
(
response
.
getMediaType
());
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"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient3_InputStream.java
0 → 100644
View file @
4a6d3dda
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
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.Response
;
public
class
Klient3_InputStream
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
root
=
client
.
target
(
Ustawienia
.
URL_SERWERA
);
Response
response
=
root
.
path
(
"products"
)
.
request
()
.
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
=
"wynik3."
+
Ustawienia
.
rozszerzenieDlaTypu
(
response
.
getMediaType
());
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"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient4_InputStream_Accept.java
0 → 100644
View file @
4a6d3dda
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
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
public
class
Klient4_InputStream_Accept
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
root
=
client
.
target
(
Ustawienia
.
URL_SERWERA
);
Response
response
=
root
.
path
(
"products"
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_XML
)
// albo "application/xml", można też bezp. w request(...)
.
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
=
"wynik4.xml"
;
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"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient5_InputStream_AcceptPDF.java
0 → 100644
View file @
4a6d3dda
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
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
public
class
Klient5_InputStream_AcceptPDF
{
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
.
URL_SERWERA
);
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
=
"wynik5.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"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient6_JsonTekstowo.java
0 → 100644
View file @
4a6d3dda
package
sklep
.
klient
;
import
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
public
class
Klient6_JsonTekstowo
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
root
=
client
.
target
(
Ustawienia
.
URL_SERWERA
);
Response
response
=
root
.
path
(
"products"
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_JSON
)
// albo "application/json", można też bezp. w request(...)
.
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
trescOdpowiedzi
=
response
.
readEntity
(
String
.
class
);
System
.
out
.
println
(
"Treść odpowiedzi:"
);
System
.
out
.
println
(
trescOdpowiedzi
);
System
.
out
.
println
(
"Gotowe"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient7_RozneKlasy.java
0 → 100644
View file @
4a6d3dda
package
sklep
.
klient
;
import
java.io.File
;
import
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
public
class
Klient7_RozneKlasy
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Startujemy..."
);
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
root
=
client
.
target
(
Ustawienia
.
URL_SERWERA
);
Response
response
=
root
.
path
(
"products"
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_JSON
)
// albo "application/json", można też bezp. w request(...)
.
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
;
}
// Do readEntity przekazuje się klasę, która wskazuje jakiego typu obiekt ma być zwrócony w Javie.
// Te same dane odebrane z serwera mogą być po stronie klienta przedstawione jako
// InputStream, Reader, String, tablica bajtów, plik (tworzny jest plik tymczsowy na dysku)
// a także, co zobaczymy od następnej wersji, obiekt "naszej klasy", np. Product.
// Object obj = response.readEntity(String.class);
// Object obj = response.readEntity(byte[].class);
Object
obj
=
response
.
readEntity
(
File
.
class
);
System
.
out
.
println
(
obj
);
System
.
out
.
println
(
"Gotowe"
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/PrzykladoweZapytanie.java
View file @
4a6d3dda
...
@@ -5,7 +5,6 @@ import javax.ws.rs.client.ClientBuilder;
...
@@ -5,7 +5,6 @@ import javax.ws.rs.client.ClientBuilder;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
public
class
PrzykladoweZapytanie
{
public
class
PrzykladoweZapytanie
{
// To pisaliśmy na żywo w tracie zajęć.
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Buduję klienta"
);
System
.
out
.
println
(
"Buduję klienta"
);
...
...
PC36-RestKlient/src/main/java/sklep/klient/Ustawienia.java
0 → 100644
View file @
4a6d3dda
package
sklep
.
klient
;
import
javax.ws.rs.core.MediaType
;
public
class
Ustawienia
{
public
static
final
String
URL_SERWERA
=
"http://localhost:8080/PC35-RestSerwer-1.0/"
;
public
static
String
rozszerzenieDlaTypu
(
MediaType
mediaType
)
{
switch
(
mediaType
.
getSubtype
())
{
case
"xml"
:
return
"xml"
;
case
"html"
:
return
"html"
;
case
"plain"
:
return
"txt"
;
case
"json"
:
return
"json"
;
case
"png"
:
return
"png"
;
case
"jpeg"
:
return
"jpg"
;
default
:
return
""
;
}
}
}
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