Created
April 26, 2020 14:51
-
-
Save ntub46010/c05fbe3b6ad004cd7a5827f3f2a34dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE) | |
public class ProductController { | |
private final List<Product> productDB = new ArrayList<>(); | |
@PostConstruct | |
private void initDB() { | |
productDB.add(new Product("B0001", "Android Development (Java)", 380)); | |
productDB.add(new Product("B0002", "Android Development (Kotlin)", 420)); | |
productDB.add(new Product("B0003", "Data Structure (Java)", 250)); | |
productDB.add(new Product("B0004", "Finance Management", 450)); | |
productDB.add(new Product("B0005", "Human Resource Management", 330)); | |
} | |
@GetMapping("/products/{id}") | |
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) { | |
Optional<Product> productOp = productDB.stream() | |
.filter(p -> p.getId().equals(id)) | |
.findFirst(); | |
if (!productOp.isPresent()) { | |
return ResponseEntity.notFound().build(); | |
} | |
Product product = productOp.get(); | |
return ResponseEntity.ok().body(product); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment