Commit fdc4b353 by Patryk Czarnik

Użycie @RestController zamiast @Controller

parent e2c4a390
package sklep.rest; package sklep.rest;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import sklep.model.Product; import sklep.model.Product;
import sklep.photo.PhotoUtil; import sklep.photo.PhotoUtil;
...@@ -13,7 +9,9 @@ import sklep.repository.ProductRepository; ...@@ -13,7 +9,9 @@ import sklep.repository.ProductRepository;
import java.util.List; import java.util.List;
@Controller // @RestController odpowiada adnotacji @Controller z dodatkową adnotacją @ResponseBody
// Inaczej mówiąc, metody nie zwracają nazw szablonów, lecz dane, które mają być odesłane do klienta.
@RestController
@RequestMapping("/rest/products") @RequestMapping("/rest/products")
public class ProductEndpoint { public class ProductEndpoint {
private ProductRepository productRepository; private ProductRepository productRepository;
...@@ -26,13 +24,11 @@ public class ProductEndpoint { ...@@ -26,13 +24,11 @@ public class ProductEndpoint {
} }
@GetMapping @GetMapping
@ResponseBody
public List<Product> getProducts() { public List<Product> getProducts() {
return productRepository.findAll(); return productRepository.findAll();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ResponseBody
public Product getProduct(@PathVariable int id) { public Product getProduct(@PathVariable int id) {
return productRepository.findById(id).orElseThrow( return productRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatusCode.valueOf(404), () -> new ResponseStatusException(HttpStatusCode.valueOf(404),
......
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