Commit f502ed42 by Patryk Czarnik

@RestController

parent bb07f89c
...@@ -4,30 +4,27 @@ import java.util.List; ...@@ -4,30 +4,27 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
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.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
@Controller @RestController
@RequestMapping("/rest/products") @RequestMapping("/rest/products")
public class ProductEndpoint { public class ProductEndpoint {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@GetMapping @GetMapping
@ResponseBody
public List<Product> readAll() { public List<Product> readAll() {
return productRepository.findAll(); return productRepository.findAll();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ResponseBody
public Product readOne(@PathVariable Integer id) { public Product readOne(@PathVariable Integer id) {
return productRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); return productRepository.findById(id).orElseThrow(() -> 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