From e51834c899ff20781295410961934eb06239385c Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 16 May 2026 21:18:32 +0530 Subject: Added spring-boot sample applications --- spring-boot/05-bank-rest-client/.gitignore | 1 + spring-boot/05-bank-rest-client/pom.xml | 28 +++++++++++++ .../main/java/com/example/spring/Application.java | 12 ++++++ .../com/example/spring/bank/account/Account.java | 27 +++++++++++++ .../spring/bank/account/AccountService.java | 24 +++++++++++ .../spring/bank/restclient/test/AccForm.java | 13 ++++++ .../bank/restclient/test/RestClientTest.java | 46 ++++++++++++++++++++++ .../src/main/resources/application.properties | 12 ++++++ .../src/main/resources/templates/form.html | 17 ++++++++ .../src/main/resources/templates/index.html | 16 ++++++++ .../src/main/resources/templates/result.html | 15 +++++++ 11 files changed, 211 insertions(+) create mode 100644 spring-boot/05-bank-rest-client/.gitignore create mode 100644 spring-boot/05-bank-rest-client/pom.xml create mode 100644 spring-boot/05-bank-rest-client/src/main/java/com/example/spring/Application.java create mode 100644 spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/Account.java create mode 100644 spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/account/AccountService.java create mode 100644 spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/AccForm.java create mode 100644 spring-boot/05-bank-rest-client/src/main/java/com/example/spring/bank/restclient/test/RestClientTest.java create mode 100644 spring-boot/05-bank-rest-client/src/main/resources/application.properties create mode 100644 spring-boot/05-bank-rest-client/src/main/resources/templates/form.html create mode 100644 spring-boot/05-bank-rest-client/src/main/resources/templates/index.html create mode 100644 spring-boot/05-bank-rest-client/src/main/resources/templates/result.html (limited to 'spring-boot/05-bank-rest-client') 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 @@ + + + 4.0.0 + + + com.example.spring.boot + base-config + 0.0.1-SNAPSHOT + ../00-config + + + bank-rest-client + + + + org.springframework.boot + spring-boot-starter-restclient + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + \ 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 @@ + + + + + Check Account Balance + + + + +

Form

+
+

Account number:

+

+
+ + + \ 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 @@ + + + + + Check Account Balance + + + + + +Access /acc + + + + + 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 @@ + + + + Check Account Balance + + + +

Result

+ +

+

+ + Try again + + \ No newline at end of file -- cgit v1.2.3