Commit c334179e by Patryk Czarnik

ResponseStatusException(NOT_FOUND)

parent fd81d314
...@@ -3,10 +3,12 @@ package sklep.rest; ...@@ -3,10 +3,12 @@ package sklep.rest;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
...@@ -26,9 +28,14 @@ public class ProductEndpoint { ...@@ -26,9 +28,14 @@ public class ProductEndpoint {
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Optional<Product> getOneProduct(@PathVariable("id") Integer id) { public Product getOneProduct(@PathVariable("id") Integer id) {
return productRepository.findById(id); Optional<Product> product = productRepository.findById(id);
if(product.isPresent()) {
return product.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment