Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
c9b76e8b
Commit
c9b76e8b
authored
May 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Klient RESTEasy - wersje zaawansowane :-)
parent
cc0634a3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
0 deletions
+122
-0
.gitignore
PC34-RestKlient/.gitignore
+1
-0
P09_RestClient_Interaktywny.java
...n/java/sklep/klient_rest/P09_RestClient_Interaktywny.java
+61
-0
P10_RestClient_PDF.java
...t/src/main/java/sklep/klient_rest/P10_RestClient_PDF.java
+60
-0
No files found.
PC34-RestKlient/.gitignore
View file @
c9b76e8b
...
...
@@ -8,3 +8,4 @@
/.idea/
/wynik*
/*.pdf
PC34-RestKlient/src/main/java/sklep/klient_rest/P09_RestClient_Interaktywny.java
0 → 100644
View file @
c9b76e8b
package
sklep
.
klient_rest
;
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
P09_RestClient_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ć"
);
}
}
}
}
PC34-RestKlient/src/main/java/sklep/klient_rest/P10_RestClient_PDF.java
0 → 100644
View file @
c9b76e8b
package
sklep
.
klient_rest
;
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
P10_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"
);
}
}
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