restore GET

djmil 2023-07-22 15:11:34 +02:00
parent 0ef130cb3b
commit 97c69cfaa8
2 changed files with 53 additions and 186 deletions

@ -1,186 +0,0 @@
{
"main": {
"id": "bfa6b0eb76264f17",
"type": "split",
"children": [
{
"id": "91a95d9b9ba4dc49",
"type": "tabs",
"children": [
{
"id": "a3660fdf5f5aceb3",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Home.md",
"mode": "source",
"source": false
}
}
},
{
"id": "ca75671fa123bc88",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Home.md",
"mode": "source",
"source": false
}
}
}
],
"currentTab": 1
}
],
"direction": "vertical"
},
"left": {
"id": "5eaa206d99edb028",
"type": "split",
"children": [
{
"id": "d007c720b5e598a0",
"type": "tabs",
"children": [
{
"id": "03171174265e0ee5",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical"
}
}
},
{
"id": "f78dd3c57a1694ca",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
}
}
},
{
"id": "ef97ee1190cc8880",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {}
}
}
]
}
],
"direction": "horizontal",
"width": 249.5
},
"right": {
"id": "3bd78c465d2a2030",
"type": "split",
"children": [
{
"id": "e39fc11f28834b41",
"type": "tabs",
"children": [
{
"id": "e0d70c2f91ae0cfb",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"file": "Home.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "11cce84d7b97ddc8",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"file": "Home.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "f8f2ce3f2694f545",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": true
}
}
},
{
"id": "58d34025bb119389",
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "Home.md"
}
}
}
],
"currentTab": 3
}
],
"direction": "horizontal",
"width": 300
},
"left-ribbon": {
"hiddenItems": {
"switcher:Open quick switcher": false,
"graph:Open graph view": false,
"canvas:Create new canvas": false,
"daily-notes:Open today's daily note": false,
"templates:Insert template": false,
"command-palette:Open command palette": false
}
},
"active": "ca75671fa123bc88",
"lastOpenFiles": [
"Pagination.md",
"Home.md",
"Database.md",
"Post.md",
"GET.md",
"Get.md",
"assets/Pasted image 20230721100304.png",
"IntegrationTests.md",
"UnitTests.md",
"assets/Pasted image 20230719152007.png",
"Pasted image 20230719102301.png",
"assets/Pasted image 20230719102322.png",
"assets",
"Pasted image 20230718185450.png",
"testPage.md",
"subfolder/Note1.md",
"_Sidebar.md",
"tests%252FSubpage.md.-.md",
"subfolder.md",
"subfolder%5C%2Fp2.md",
"subfolder"
]
}

53
Get.md Normal file

@ -0,0 +1,53 @@
`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<CashCard> 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<String> 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