Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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_20230617
Commits
7cd3cc68
Commit
7cd3cc68
authored
Jul 16, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Waluty REST
parent
0c0e1871
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
WalutyRest.java
...ing/src/main/java/com/example/demo/waluty/WalutyRest.java
+88
-0
index.html
PC25-Spring/src/main/resources/templates/index.html
+2
-0
No files found.
PC25-Spring/src/main/java/com/example/demo/waluty/WalutyRest.java
0 → 100644
View file @
7cd3cc68
package
com
.
example
.
demo
.
waluty
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigDecimal
;
import
java.net.URL
;
import
java.time.LocalDate
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.parsers.ParserConfigurationException
;
import
javax.xml.xpath.XPath
;
import
javax.xml.xpath.XPathConstants
;
import
javax.xml.xpath.XPathExpressionException
;
import
javax.xml.xpath.XPathFactory
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.w3c.dom.Document
;
import
org.w3c.dom.Node
;
import
org.w3c.dom.NodeList
;
import
org.xml.sax.SAXException
;
@RestController
@RequestMapping
(
"/waluty.rest"
)
public
class
WalutyRest
{
@GetMapping
public
TabelaWalut
wyswietlWaluty
()
{
TabelaWalut
tabela
=
pobierzTabele
(
null
);
return
tabela
;
}
@GetMapping
(
"/{data}"
)
public
TabelaWalut
wyswietlWaluty
(
@PathVariable
String
data
)
{
TabelaWalut
tabela
=
pobierzTabele
(
data
);
return
tabela
;
}
private
TabelaWalut
pobierzTabele
(
String
data
)
{
String
adres
=
"https://api.nbp.pl/api/exchangerates/tables/a"
;
if
(
data
!=
null
)
{
adres
+=
"/"
+
data
;
}
adres
+=
"?format=xml"
;
try
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
doc
=
null
;
URL
url
=
new
URL
(
adres
);
try
(
InputStream
in
=
url
.
openStream
())
{
doc
=
db
.
parse
(
in
);
}
XPathFactory
xpf
=
XPathFactory
.
newInstance
();
XPath
xpath
=
xpf
.
newXPath
();
// String numer = xpath.evaluate(
// "/ArrayOfExchangeRatesTable/ExchangeRatesTable/No", doc);
String
nazwaTabeli
=
xpath
.
evaluate
(
"//Table"
,
doc
);
String
numerTabeli
=
xpath
.
evaluate
(
"//No"
,
doc
);
LocalDate
dataTabeli
=
LocalDate
.
parse
(
xpath
.
evaluate
(
"//EffectiveDate"
,
doc
));
TabelaWalut
tabela
=
new
TabelaWalut
(
nazwaTabeli
,
numerTabeli
,
dataTabeli
);
NodeList
rates
=
(
NodeList
)
xpath
.
evaluate
(
"//Rate"
,
doc
,
XPathConstants
.
NODESET
);
final
int
n
=
rates
.
getLength
();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Node
rate
=
rates
.
item
(
i
);
String
kod
=
xpath
.
evaluate
(
"Code"
,
rate
);
String
nazwa
=
xpath
.
evaluate
(
"Currency"
,
rate
);
BigDecimal
kurs
=
new
BigDecimal
(
xpath
.
evaluate
(
"Mid"
,
rate
));
Waluta
waluta
=
new
Waluta
(
kod
,
nazwa
,
kurs
);
tabela
.
dodajWalute
(
waluta
);
}
return
tabela
;
}
catch
(
ParserConfigurationException
|
SAXException
|
XPathExpressionException
|
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
PC25-Spring/src/main/resources/templates/index.html
View file @
7cd3cc68
...
...
@@ -38,6 +38,8 @@
<ul>
<li><a
th:href=
"@{/kalkulator}"
>
Kalkulator
</a></li>
<li><a
th:href=
"@{/waluty}"
>
Kursy walut
</a></li>
<li><a
th:href=
"@{/waluty.rest}"
>
Waluty REST
</a>
- bieżące
</li>
<li><a
th:href=
"@{/waluty.rest/2008-08-08}"
>
Waluty REST
</a>
- archiwalne
</li>
</ul>
</body>
...
...
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