Update GET
							parent
							
								
									fb97794e5c
								
							
						
					
					
						commit
						1cd57754eb
					
				
							
								
								
									
										106
									
								
								GET.md
									
									
									
									
									
								
							
							
								
								
								
								
									
									
								
								
								
							
						
						
									
										106
									
								
								GET.md
									
									
									
									
									
								
							@ -1,52 +1,54 @@
 | 
				
			|||||||
`src/main/java/djmil/cashcard/CashCardController.java`
 | 
					`src/main/java/djmil/cashcard/CashCardController.java`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```java
 | 
					```java
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package example.cashcard;
 | 
					package example.cashcard;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.GetMapping;
 | 
					import org.springframework.web.bind.annotation.GetMapping;
 | 
				
			||||||
import org.springframework.web.bind.annotation.PathVariable;
 | 
					import org.springframework.web.bind.annotation.PathVariable;
 | 
				
			||||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
					import org.springframework.web.bind.annotation.RequestMapping;
 | 
				
			||||||
import org.springframework.web.bind.annotation.RestController;
 | 
					import org.springframework.web.bind.annotation.RestController;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
public class CashCardController {
 | 
					public class CashCardController {
 | 
				
			||||||
	@GetMapping("/cashcards/{requestedId}")
 | 
						@GetMapping("/cashcards/{requestedId}")
 | 
				
			||||||
	public ResponseEntity<CashCard> findById(@PathVariable Long requestedId) {
 | 
						public ResponseEntity<CashCard> findById(@PathVariable Long requestedId) {
 | 
				
			||||||
		if (requestedId.equals(99L)) {
 | 
							if (requestedId.equals(99L)) {
 | 
				
			||||||
		    CashCard cashCard = new CashCard(99L, 123.45);
 | 
							    CashCard cashCard = new CashCard(99L, 123.45);
 | 
				
			||||||
	        return ResponseEntity.ok(cashCard);
 | 
						        return ResponseEntity.ok(cashCard);
 | 
				
			||||||
	    } else {
 | 
						    } else {
 | 
				
			||||||
	        return ResponseEntity.notFound().build();
 | 
						        return ResponseEntity.notFound().build();
 | 
				
			||||||
	    }
 | 
						    }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# @GetMapping 
 | 
					# @GetMapping 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Needs the URI Path and tells Spring to route `GET` requests strictly to the `findById` method. 
 | 
					Needs the URI Path and tells Spring to route `GET` requests strictly to the `findById` method. 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Alternative variant for providing URI mapping for `@RestConttroller`
 | 
					## Alternative variant for providing URI mapping for `@RestConttroller`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```java
 | 
					```java
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@RequestMapping("/cashcards")
 | 
					@RequestMapping("/cashcards")
 | 
				
			||||||
public class CashCardController {
 | 
					public class CashCardController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   @GetMapping("/{requestedId}")
 | 
					   @GetMapping("/{requestedId}")
 | 
				
			||||||
   public ResponseEntity<String> findById() {
 | 
					   public ResponseEntity<String> findById() {
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# @PathVariable
 | 
					# @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.
 | 
					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
 | 
					# 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`.
 | 
					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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Testing `GET` and other REST request is considerate to be [[IntegrationTests]]. 
 | 
					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
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user