Skip to content

Instantly share code, notes, and snippets.

@ntub46010
Created April 26, 2020 14:51
Show Gist options
  • Save ntub46010/c05fbe3b6ad004cd7a5827f3f2a34dbf to your computer and use it in GitHub Desktop.
Save ntub46010/c05fbe3b6ad004cd7a5827f3f2a34dbf to your computer and use it in GitHub Desktop.
@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