summaryrefslogtreecommitdiff
path: root/spring-boot/05-bank-rest-client
diff options
context:
space:
mode:
Diffstat (limited to 'spring-boot/05-bank-rest-client')
-rw-r--r--spring-boot/05-bank-rest-client/.gitignore1
-rw-r--r--spring-boot/05-bank-rest-client/pom.xml28
-rw-r--r--spring-boot/05-bank-rest-client/src/main/java/com/example/spring/Application.java12
-rw-r--r--spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/Account.java27
-rw-r--r--spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/AccountService.java24
-rw-r--r--spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/AccForm.java13
-rw-r--r--spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/RestClientTest.java46
-rw-r--r--spring-boot/05-bank-rest-client/src/main/resources/application.properties12
-rw-r--r--spring-boot/05-bank-rest-client/src/main/resources/templates/form.html17
-rw-r--r--spring-boot/05-bank-rest-client/src/main/resources/templates/index.html16
-rw-r--r--spring-boot/05-bank-rest-client/src/main/resources/templates/result.html15
11 files changed, 211 insertions, 0 deletions
diff --git a/spring-boot/05-bank-rest-client/.gitignore b/spring-boot/05-bank-rest-client/.gitignore
new file mode 100644
index 0000000..eb5a316
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/spring-boot/05-bank-rest-client/pom.xml b/spring-boot/05-bank-rest-client/pom.xml
new file mode 100644
index 0000000..520bf43
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/pom.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>com.example.spring.boot</groupId>
+ <artifactId>base-config</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <relativePath>../00-config</relativePath>
+ </parent>
+
+ <artifactId>bank-rest-client</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-restclient</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
+ </dependency>
+ </dependencies>
+
+</project> \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/Application.java b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/Application.java
new file mode 100644
index 0000000..4393f3f
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/Application.java
@@ -0,0 +1,12 @@
+package com.example.spring;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+ public static void main(String[] args) throws Exception {
+ SpringApplication.run(Application.class, args);
+ }
+} \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/Account.java b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/Account.java
new file mode 100644
index 0000000..f9789e4
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/Account.java
@@ -0,0 +1,27 @@
+package com.example.spring.bank.account;
+
+import java.math.BigDecimal;
+
+public class Account {
+ private int accountNumber;
+
+ private BigDecimal balance;
+
+ public int getAccountNumber() {
+ return accountNumber;
+ }
+ public void setAccountNumber(int accountNumber) {
+ this.accountNumber = accountNumber;
+ }
+ public BigDecimal getBalance() {
+ return balance;
+ }
+ public void setBalance(BigDecimal balance) {
+ this.balance = balance;
+ }
+
+ @Override
+ public String toString() {
+ return "Account [accountNumber=" + accountNumber + ", balance=" + balance + "]";
+ }
+} \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/AccountService.java b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/AccountService.java
new file mode 100644
index 0000000..c7b3804
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/AccountService.java
@@ -0,0 +1,24 @@
+package com.example.spring.bank.account;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.restclient.RestTemplateBuilder;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+@Service
+public class AccountService {
+
+ @Value("${rest.baseURL}")
+ private String restBaseURL;
+
+ private final RestTemplate restTemplate;
+
+ public AccountService(RestTemplateBuilder restTemplateBuilder) {
+ this.restTemplate = restTemplateBuilder.build();
+ }
+
+ public Account getAccount(Integer accno) {
+ return this.restTemplate.getForObject(restBaseURL + "/account/{accno}", Account.class, accno);
+ }
+
+} \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/AccForm.java b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/AccForm.java
new file mode 100644
index 0000000..9b58dae
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/AccForm.java
@@ -0,0 +1,13 @@
+package com.example.spring.bank.restclient.test;
+
+public class AccForm {
+
+ private Integer accNo;
+
+ public Integer getAccNo() {
+ return accNo;
+ }
+ public void setAccNo(Integer accNo) {
+ this.accNo = accNo;
+ }
+}
diff --git a/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/RestClientTest.java b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/RestClientTest.java
new file mode 100644
index 0000000..adf4755
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/RestClientTest.java
@@ -0,0 +1,46 @@
+package com.example.spring.bank.restclient.test;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.servlet.ModelAndView;
+
+import com.example.spring.bank.account.Account;
+import com.example.spring.bank.account.AccountService;
+
+@Controller
+public class RestClientTest {
+
+ @Autowired
+ private AccountService accountService;
+
+ @GetMapping("/acc")
+ public String accountForm(Model model) {
+ model.addAttribute("account", new AccForm());
+ return "form";
+ }
+
+ @PostMapping("/acc")
+ public ModelAndView accountSubmission(@ModelAttribute AccForm accForm) {
+
+ Account account = accountService.getAccount(accForm.getAccNo());
+
+ ModelAndView mav = new ModelAndView();
+ mav.addObject("account", account);
+
+ mav.setViewName("result");
+
+ return mav;
+ }
+
+
+ @RequestMapping("/")
+ public String checkAccount() {
+ return "index";
+ }
+
+}
diff --git a/spring-boot/05-bank-rest-client/src/main/resources/application.properties b/spring-boot/05-bank-rest-client/src/main/resources/application.properties
new file mode 100644
index 0000000..cf862c6
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/resources/application.properties
@@ -0,0 +1,12 @@
+# Possible locations of this file:
+# (Properties defined in locations higher in the list override those defined in lower locations)
+#
+# A /config subdirectory of the current directory
+# The current directory
+# A classpath /config package
+# The classpath root
+#
+
+server.port = 8081
+
+rest.baseURL=http://localhost:8080 \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/resources/templates/form.html b/spring-boot/05-bank-rest-client/src/main/resources/templates/form.html
new file mode 100644
index 0000000..7927b92
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/resources/templates/form.html
@@ -0,0 +1,17 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+
+<head>
+ <title>Check Account Balance</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+
+<body>
+ <h1>Form</h1>
+ <form action="#" th:action="@{/acc}" th:object="${account}" method="post">
+ <p>Account number: <input type="text" th:field="*{accNo}" /></p>
+ <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
+ </form>
+</body>
+
+</html> \ No newline at end of file
diff --git a/spring-boot/05-bank-rest-client/src/main/resources/templates/index.html b/spring-boot/05-bank-rest-client/src/main/resources/templates/index.html
new file mode 100644
index 0000000..f30a2c0
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/resources/templates/index.html
@@ -0,0 +1,16 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+
+<head>
+ <title>Check Account Balance</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+
+<body>
+
+Access <a href="/acc">/acc</a>
+
+</body>
+
+</html>
+
diff --git a/spring-boot/05-bank-rest-client/src/main/resources/templates/result.html b/spring-boot/05-bank-rest-client/src/main/resources/templates/result.html
new file mode 100644
index 0000000..15393be
--- /dev/null
+++ b/spring-boot/05-bank-rest-client/src/main/resources/templates/result.html
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+ <title>Check Account Balance</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+</head>
+<body>
+ <h1>Result</h1>
+
+ <p th:text="'Account number: ' + ${account.accountNumber}" />
+ <p th:text="'Balance: ' + ${account.balance}" />
+
+ <a href="/acc">Try again</a>
+</body>
+</html> \ No newline at end of file