Skip to content

Instantly share code, notes, and snippets.

@ntub46010
Created April 26, 2020 14:53
Show Gist options
  • Save ntub46010/f57f3c030becc1fdf0e3a8bff821de73 to your computer and use it in GitHub Desktop.
Save ntub46010/f57f3c030becc1fdf0e3a8bff821de73 to your computer and use it in GitHub Desktop.
@PostMapping("/products")
public ResponseEntity<Product> createProduct(@RequestBody Product request) {
boolean isIdDuplicated = productDB.stream()
.anyMatch(p -> p.getId().equals(request.getId()));
if (isIdDuplicated) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
Product product = new Product();
product.setId(request.getId());
product.setName(request.getName());
product.setPrice(request.getPrice());
productDB.add(product);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(product.getId())
.toUri();
return ResponseEntity.created(location).body(product);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment