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
718e3a67
Commit
718e3a67
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wielomodulowy-RestKlient
parent
d414a2ad
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
564 additions
and
0 deletions
+564
-0
pom.xml
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/pom.xml
+40
-0
Klient1_URL.java
...wy-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
Klient8_JSON.java
...y-RestKlient/src/main/java/sklep/klient/Klient8_JSON.java
+39
-0
Klient9_JAXB.java
...y-RestKlient/src/main/java/sklep/klient/Klient9_JAXB.java
+39
-0
Klient_Interaktywny.java
...lient/src/main/java/sklep/klient/Klient_Interaktywny.java
+62
-0
PrzykladoweZapytanie.java
...ient/src/main/java/sklep/klient/PrzykladoweZapytanie.java
+26
-0
Ustawienia.java
...owy-RestKlient/src/main/java/sklep/klient/Ustawienia.java
+21
-0
pom.xml
PC38-Wielomodulowy/pom.xml
+1
-0
No files found.
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/pom.xml
0 → 100644
View file @
718e3a67
<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.wielomodulowy
</groupId>
<artifactId>
PC38-Wielomodulowy
</artifactId>
<version>
1.0
</version>
</parent>
<artifactId>
PC38-Wielomodulowy-RestKlient
</artifactId>
<properties>
<resteasy.version>
5.0.4.Final
</resteasy.version>
</properties>
<dependencies>
<dependency>
<groupId>
${project.groupId}
</groupId>
<artifactId>
PC38-Wielomodulowy-Model
</artifactId>
<version>
${project.version}
</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
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient1_URL.java
0 → 100644
View file @
718e3a67
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
();
}
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient2_InputStream.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient3_InputStream.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient4_InputStream_Accept.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient5_InputStream_AcceptPDF.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient6_JsonTekstowo.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient7_RozneKlasy.java
0 → 100644
View file @
718e3a67
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"
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient8_JSON.java
0 → 100644
View file @
718e3a67
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
;
import
sklep.model.ProductBezAdnotacji
;
public
class
Klient8_JSON
{
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/1"
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_JSON
)
.
buildGet
()
.
invoke
();
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
;
}
ProductBezAdnotacji
product
=
response
.
readEntity
(
ProductBezAdnotacji
.
class
);
System
.
out
.
println
(
"Mam produkt: "
+
product
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient9_JAXB.java
0 → 100644
View file @
718e3a67
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
;
import
sklep.model.Product
;
public
class
Klient9_JAXB
{
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/1"
)
.
request
()
.
accept
(
MediaType
.
APPLICATION_XML
)
.
buildGet
()
.
invoke
();
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
;
}
Product
product
=
response
.
readEntity
(
Product
.
class
);
System
.
out
.
println
(
"Mam produkt: "
+
product
);
System
.
out
.
println
(
"Cena: "
+
product
.
getPrice
());
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Klient_Interaktywny.java
0 → 100644
View file @
718e3a67
package
sklep
.
klient
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.Entity
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
sklep.model.Product
;
public
class
Klient_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
.
URL_SERWERA
)
.
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_XML
)
.
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ć"
);
}
}
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/PrzykladoweZapytanie.java
0 → 100644
View file @
718e3a67
package
sklep
.
klient
;
import
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.core.Response
;
public
class
PrzykladoweZapytanie
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Buduję klienta"
);
Client
client
=
ClientBuilder
.
newClient
();
System
.
out
.
println
(
"Wysyłam zapytanie..."
);
Response
response
=
client
.
target
(
"http://localhost:8080/PC35-RestSerwer-1.0/products.json/1"
)
.
request
().
buildGet
().
invoke
();
System
.
out
.
println
(
"Mam odpowiedź: "
+
response
);
System
.
out
.
println
(
"Kod: "
+
response
.
getStatus
());
System
.
out
.
println
(
"Content-type: "
+
response
.
getMediaType
());
String
trescOdpowiedzi
=
response
.
readEntity
(
String
.
class
);
System
.
out
.
println
(
"Treść odpowiedzi:"
);
System
.
out
.
println
(
trescOdpowiedzi
);
}
}
PC38-Wielomodulowy/PC38-Wielomodulowy-RestKlient/src/main/java/sklep/klient/Ustawienia.java
0 → 100644
View file @
718e3a67
package
sklep
.
klient
;
import
javax.ws.rs.core.MediaType
;
public
class
Ustawienia
{
public
static
final
String
URL_SERWERA
=
"http://localhost:8080/PC38-Wielomodulowy-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
""
;
}
}
}
PC38-Wielomodulowy/pom.xml
View file @
718e3a67
...
...
@@ -14,5 +14,6 @@
<module>
PC38-Wielomodulowy-Model
</module>
<module>
PC38-Wielomodulowy-BazaDanych
</module>
<module>
PC38-Wielomodulowy-RestSerwer
</module>
<module>
PC38-Wielomodulowy-RestKlient
</module>
</modules>
</project>
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