Created
April 20, 2020 15:12
-
-
Save ntub46010/454a745d1b9af692839fadab5dc01b10 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("/products") | |
public class ProductController { | |
@Autowired | |
private ProductService productService; | |
@GetMapping("/{id}") | |
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) { | |
Product product = productService.getProduct(id); | |
return ResponseEntity.ok(product); | |
} | |
@PostMapping | |
public ResponseEntity<Product> createProduct(@RequestBody Product request) { | |
Product product = productService.createProduct(request); | |
URI location = ServletUriComponentsBuilder | |
.fromCurrentRequest() | |
.path("/{id}") | |
.buildAndExpand(product.getId()) | |
.toUri(); | |
return ResponseEntity.created(location).body(product); | |
} | |
@PutMapping("/{id}") | |
public ResponseEntity<Product> replaceProduct( | |
@PathVariable("id") String id, @RequestBody Product request) { | |
Product product = productService.replaceProduct(id, request); | |
return ResponseEntity.ok(product); | |
} | |
@DeleteMapping("/{id}") | |
public ResponseEntity deleteProduct(@PathVariable("id") String id) { | |
productService.deleteProduct(id); | |
return ResponseEntity.noContent().build(); | |
} | |
@GetMapping | |
public ResponseEntity<List<Product>> getProducts(@ModelAttribute ProductQueryParameter param) { | |
List<Product> products = productService.getProducts(param); | |
return ResponseEntity.ok(products); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment