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
84be055d
Commit
84be055d
authored
Nov 09, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RestEasyClient z wykorzystaniem klas modelu
parent
40b682b4
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
238 additions
and
1 deletions
+238
-1
pom.xml
PC36-RestKlient/pom.xml
+12
-1
Klient14_RestClient_PDF.java
...nt/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
No files found.
PC36-RestKlient/pom.xml
View file @
84be055d
...
...
@@ -10,6 +10,7 @@
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.target>
17
</maven.compiler.target>
<maven.compiler.source>
17
</maven.compiler.source>
<resteasy.version>
6.2.6.Final
</resteasy.version>
</properties>
<dependencies>
...
...
@@ -31,7 +32,17 @@
<dependency>
<groupId>
org.jboss.resteasy
</groupId>
<artifactId>
resteasy-client
</artifactId>
<version>
6.2.6.Final
</version>
<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>
...
...
PC36-RestKlient/src/main/java/rest_klient/Klient14_RestClient_PDF.java
0 → 100644
View file @
84be055d
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"
);
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient21_RestClient_JAXB.java
0 → 100644
View file @
84be055d
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
);
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient22_RestClient_JSON.java
0 → 100644
View file @
84be055d
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
);
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient23_RestClient_JSON_JedenProdukt.java
0 → 100644
View file @
84be055d
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
);
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient24_Interaktywna_Edycja.java
0 → 100644
View file @
84be055d
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ć"
);
}
}
}
}
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