From 1cd57754eb5a5350ee3eb8e2b6221aec5a1199d7 Mon Sep 17 00:00:00 2001 From: djmil Date: Wed, 19 Jul 2023 14:33:22 +0200 Subject: [PATCH] Update GET --- GET.md | 106 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/GET.md b/GET.md index 63c7af5..ca608a3 100644 --- a/GET.md +++ b/GET.md @@ -1,52 +1,54 @@ -`src/main/java/djmil/cashcard/CashCardController.java` - -```java - -package example.cashcard; - -import org.springframework.http.ResponseEntity; -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; - -@RestController -public class CashCardController { - @GetMapping("/cashcards/{requestedId}") - public ResponseEntity findById(@PathVariable Long requestedId) { - if (requestedId.equals(99L)) { - CashCard cashCard = new CashCard(99L, 123.45); - return ResponseEntity.ok(cashCard); - } else { - return ResponseEntity.notFound().build(); - } - } -} -``` - -# @GetMapping - -Needs the URI Path and tells Spring to route `GET` requests strictly to the `findById` method. - -## Alternative variant for providing URI mapping for `@RestConttroller` - -```java -@RestController -@RequestMapping("/cashcards") -public class CashCardController { - - @GetMapping("/{requestedId}") - public ResponseEntity findById() { -``` - -# @PathVariable - -Spring needs to know how to get the value of the `requestedId` parameter. This is done using the `@PathVariable` annotation. The fact that the parameter name matches the `{requestedId}` text *(URI Path)* within the `@GetMapping` parameter allows Spring to assign (inject) the correct value to the `requestedId` variable. - -# ResponseEntity - -REST says that the Response needs to contain a Cash Card in its body, and a Response code of 200 (OK). Spring Web provides the `ResponseEntity` class for this purpose. It also provides several utility methods to produce Response Entities. Here `ResponseEntity` used to create a Response with code **200 (OK)**, and a body containing a `CashCard`. - -# Testing - -Testing `GET` and other REST request is considerate to be [[IntegrationTests]]. \ No newline at end of file +`src/main/java/djmil/cashcard/CashCardController.java` + +```java + +package example.cashcard; + +import org.springframework.http.ResponseEntity; +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; + +@RestController +public class CashCardController { + @GetMapping("/cashcards/{requestedId}") + public ResponseEntity findById(@PathVariable Long requestedId) { + if (requestedId.equals(99L)) { + CashCard cashCard = new CashCard(99L, 123.45); + return ResponseEntity.ok(cashCard); + } else { + return ResponseEntity.notFound().build(); + } + } +} +``` + +# @GetMapping + +Needs the URI Path and tells Spring to route `GET` requests strictly to the `findById` method. + +## Alternative variant for providing URI mapping for `@RestConttroller` + +```java +@RestController +@RequestMapping("/cashcards") +public class CashCardController { + + @GetMapping("/{requestedId}") + public ResponseEntity findById() { +``` + +# @PathVariable + +Spring needs to know how to get the value of the `requestedId` parameter. This is done using the `@PathVariable` annotation. The fact that the parameter name matches the `{requestedId}` text *(URI Path)* within the `@GetMapping` parameter allows Spring to assign (inject) the correct value to the `requestedId` variable. + +# ResponseEntity + +REST says that the Response needs to contain a Cash Card in its body, and a Response code of 200 (OK). Spring Web provides the `ResponseEntity` class for this purpose. It also provides several utility methods to produce Response Entities. Here `ResponseEntity` used to create a Response with code **200 (OK)**, and a body containing a `CashCard`. + +# Testing + +Testing `GET` and other REST request is considerate to be [[IntegrationTests]]. + +Also, after running the application, you can use browser (or any other web-testing tool) by visiting http://localhost:8080/cashcards/99 \ No newline at end of file