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
c779c150
Commit
c779c150
authored
Nov 09, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady używające Jakarta JSON
parent
2e948db5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
185 additions
and
2 deletions
+185
-2
pom.xml
PC36-RestKlient/pom.xml
+19
-2
Klient02_URL_JSON.java
...stKlient/src/main/java/rest_klient/Klient02_URL_JSON.java
+33
-0
Klient05_HttpClient_Accept.java
...src/main/java/rest_klient/Klient05_HttpClient_Accept.java
+31
-0
Klient06_HttpClient_JSON.java
...t/src/main/java/rest_klient/Klient06_HttpClient_JSON.java
+49
-0
Klient07_HttpClient_JSON_Lista.java
...main/java/rest_klient/Klient07_HttpClient_JSON_Lista.java
+53
-0
No files found.
PC36-RestKlient/pom.xml
View file @
c779c150
...
@@ -12,4 +12,22 @@
...
@@ -12,4 +12,22 @@
<maven.compiler.source>
17
</maven.compiler.source>
<maven.compiler.source>
17
</maven.compiler.source>
</properties>
</properties>
</project>
<dependencies>
\ No newline at end of file
<!-- implementacje technologii Jakarta JSON P
- Glassfish to starsza wersja,
- Eclipse Parsson to nowsza -->
<!--
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>2.0.1</version>
</dependency>
-->
<dependency>
<groupId>
org.eclipse.parsson
</groupId>
<artifactId>
parsson
</artifactId>
<version>
1.1.4
</version>
</dependency>
</dependencies>
</project>
PC36-RestKlient/src/main/java/rest_klient/Klient02_URL_JSON.java
0 → 100644
View file @
c779c150
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient02_URL_JSON
{
public
static
void
main
(
String
[]
args
)
{
try
{
URL
url
=
new
URL
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
try
(
InputStream
inputStream
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
inputStream
))
{
JsonArray
array
=
reader
.
readArray
();
// System.out.println(array);
for
(
JsonValue
jsonValue
:
array
)
{
//System.out.println(jsonValue);
System
.
out
.
println
(
jsonValue
.
asJsonObject
().
getString
(
"productName"
));
System
.
out
.
println
(
" opis: "
+
jsonValue
.
asJsonObject
().
getString
(
"description"
,
""
));
System
.
out
.
println
(
" cena: "
+
jsonValue
.
asJsonObject
().
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient05_HttpClient_Accept.java
0 → 100644
View file @
c779c150
package
rest_klient
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
public
class
Klient05_HttpClient_Accept
{
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products"
);
// W tej wersji do zapytania dodajemy nagłówek Accept
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"text/plain"
)
.
build
();
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient06_HttpClient_JSON.java
0 → 100644
View file @
c779c150
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
public
class
Klient06_HttpClient_JSON
{
/* W tej wersji używamy technologii Jakarta JSON P.
*
* Widzimy drzewo danych jsonowych i jego elementytakie jak JsonObject, JsonArray, ...
* Mamy dostęp do poszczególnych pól.
*/
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products/1"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
)
.
header
(
"Accept"
,
"application/json"
)
.
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
JsonReader
reader
=
Json
.
createReader
(
response
.
body
());
JsonObject
product
=
reader
.
readObject
();
System
.
out
.
println
(
"Pobrany obiekt jsonowy: "
+
product
);
String
nazwa
=
product
.
getString
(
"productName"
);
String
opis
=
product
.
getString
(
"description"
,
"BRAK OPISU"
);
BigDecimal
cena
=
product
.
getJsonNumber
(
"price"
).
bigDecimalValue
();
System
.
out
.
println
(
nazwa
+
" za cenę "
+
cena
+
" , opis: "
+
opis
);
reader
.
close
();
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient07_HttpClient_JSON_Lista.java
0 → 100644
View file @
c779c150
package
rest_klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
Klient07_HttpClient_JSON_Lista
{
public
static
void
main
(
String
[]
args
)
{
try
{
JsonArray
array
=
pobierzJsona
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
for
(
JsonValue
jsonValue
:
array
)
{
JsonObject
jsonObject
=
jsonValue
.
asJsonObject
();
System
.
out
.
println
(
jsonObject
.
getString
(
"productName"
));
if
(
jsonObject
.
containsKey
(
"description"
))
{
System
.
out
.
println
(
" opis: "
+
jsonObject
.
getString
(
"description"
,
""
));
}
System
.
out
.
println
(
" cena: "
+
jsonObject
.
getJsonNumber
(
"price"
).
bigDecimalValue
());
}
}
catch
(
IOException
|
InterruptedException
|
URISyntaxException
e
)
{
e
.
printStackTrace
();
}
}
private
static
JsonArray
pobierzJsona
(
String
adres
)
throws
IOException
,
InterruptedException
,
URISyntaxException
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
URI
uri
=
new
URI
(
adres
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
InputStream
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofInputStream
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
return
wczytajJsona
(
response
.
body
());
}
private
static
JsonArray
wczytajJsona
(
InputStream
input
)
{
try
(
JsonReader
reader
=
Json
.
createReader
(
input
))
{
return
reader
.
readArray
();
}
}
}
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