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
66e3cfbe
Commit
66e3cfbe
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady klienta REST wykorzystujące mapowanie na obiekty
parent
a01a67cf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
244 additions
and
0 deletions
+244
-0
Klient8_JSON.java
PC36-RestKlient/src/main/java/sklep/klient/Klient8_JSON.java
+39
-0
Klient9_JAXB.java
PC36-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
ProductBezAdnotacji.java
...Klient/src/main/java/sklep/model/ProductBezAdnotacji.java
+104
-0
No files found.
PC36-RestKlient/src/main/java/sklep/klient/Klient8_JSON.java
0 → 100644
View file @
66e3cfbe
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
);
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient9_JAXB.java
0 → 100644
View file @
66e3cfbe
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
());
}
}
PC36-RestKlient/src/main/java/sklep/klient/Klient_Interaktywny.java
0 → 100644
View file @
66e3cfbe
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ć"
);
}
}
}
}
PC36-RestKlient/src/main/java/sklep/model/ProductBezAdnotacji.java
0 → 100644
View file @
66e3cfbe
package
sklep
.
model
;
import
java.math.BigDecimal
;
import
java.util.Objects
;
// W tej wersji nie ma adnotacji JAXB (@Xml...), aby nie wpływały na działanie JSONa.
public
class
ProductBezAdnotacji
{
private
Integer
productId
;
private
String
productName
;
private
BigDecimal
price
;
private
BigDecimal
vat
;
private
String
description
;
public
ProductBezAdnotacji
()
{
}
public
ProductBezAdnotacji
(
Integer
productId
,
String
productName
,
BigDecimal
price
,
BigDecimal
vat
,
String
description
)
{
this
.
productId
=
productId
;
this
.
productName
=
productName
;
this
.
price
=
price
;
this
.
vat
=
vat
;
this
.
description
=
description
;
}
public
Integer
getProductId
()
{
return
productId
;
}
public
void
setProductId
(
Integer
productId
)
{
this
.
productId
=
productId
;
}
public
String
getProductName
()
{
return
productName
;
}
public
void
setProductName
(
String
productName
)
{
this
.
productName
=
productName
;
}
public
BigDecimal
getPrice
()
{
return
price
;
}
public
void
setPrice
(
BigDecimal
price
)
{
this
.
price
=
price
;
}
public
BigDecimal
getVat
()
{
return
vat
;
}
public
void
setVat
(
BigDecimal
vat
)
{
this
.
vat
=
vat
;
}
public
String
getDescription
()
{
return
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
description
,
price
,
vat
,
productId
,
productName
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
ProductBezAdnotacji
other
=
(
ProductBezAdnotacji
)
obj
;
return
Objects
.
equals
(
productId
,
other
.
productId
)
&&
Objects
.
equals
(
productName
,
other
.
productName
)
&&
Objects
.
equals
(
price
,
other
.
price
)
&&
Objects
.
equals
(
vat
,
other
.
vat
)
&&
Objects
.
equals
(
description
,
other
.
description
);
}
@Override
public
String
toString
()
{
return
"Product [productId="
+
productId
+
", productName="
+
productName
+
", price="
+
price
+
", vat="
+
vat
+
", description="
+
description
+
"]"
;
}
public
String
toHtml
()
{
return
String
.
format
(
"<div class='product'>"
+
"<h2>%s</h2>"
+
"<p>(nr %d)</p>"
+
"<p>Cena: <strong>%,.2f PLN</strong></p>"
+
"<p>%s</p>"
+
"</div>"
,
getProductName
(),
getProductId
(),
getPrice
(),
getDescription
());
}
}
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