Update GET
							parent
							
								
									fb97794e5c
								
							
						
					
					
						commit
						1cd57754eb
					
				
							
								
								
									
										106
									
								
								GET.md
									
									
									
									
									
								
							
							
								
								
								
								
									
									
								
								
								
							
						
						
									
										106
									
								
								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<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]]. 
 | 
			
		||||
`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
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user