Skip to content

Commit 3ad8638

Browse files
committed
Tutorial 6: De API uitbreiden
Nu al onze data uit een echte database komt, gaan we kijken hoe we zelf nieuwe data kunnen toevoegen via de API. We kijken hiervoor naar de HTTP POST methode, hoe we deze kunnen testen met Postman en hoe we data kunnen valideren die de gebruiker invoert. https://tweakers.net/advertorials/onlinejavaacademy2/tutorial6/ === Extra toevoeging op Products.java is de annotatie "@SiZe(min=1)" bij de property name
1 parent abb2e52 commit 3ad8638

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

src/main/java/tutorial/api/controllers/ProductController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ public Product show(@PathVariable("productId") long productId) {
2424
return productRepository.findOne(productId);
2525
}
2626

27+
@RequestMapping(value="/products", method = RequestMethod.POST)
28+
public void insert(@RequestParam("name") String name,
29+
@RequestParam("price") BigDecimal price) {
30+
Product product = new Product(name, price);
31+
productRepository.save(product);
32+
}
2733
}

src/main/java/tutorial/api/controllers/ProductListController.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package tutorial.api.controllers;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.web.bind.annotation.PathVariable;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RequestMethod;
7-
import org.springframework.web.bind.annotation.RestController;
4+
import org.springframework.web.bind.annotation.*;
5+
import tutorial.persistence.models.Product;
86
import tutorial.persistence.models.ProductList;
97
import tutorial.persistence.repositories.ProductListRepository;
8+
import tutorial.persistence.repositories.ProductRepository;
109

1110
import java.util.List;
1211

@@ -15,6 +14,8 @@ public class ProductListController extends ApiController {
1514

1615
@Autowired
1716
ProductListRepository productListRepository;
17+
@Autowired
18+
ProductRepository productRepository;
1819

1920
@RequestMapping(value="/lists", method= RequestMethod.GET)
2021
public List<ProductList> showAll() {
@@ -26,4 +27,19 @@ public ProductList show(@PathVariable("productListId") long productListId) {
2627
return productListRepository.findOne(productListId);
2728
}
2829

30+
@RequestMapping(value = "/lists", method = RequestMethod.POST)
31+
public void insert(@RequestParam("name") String name) {
32+
ProductList productList = new ProductList(name);
33+
productListRepository.save(productList);
34+
}
35+
36+
@RequestMapping(path = "/lists/{listId}/products", method = RequestMethod.POST)
37+
public void addProduct(@PathVariable("listId") long listId,
38+
@RequestParam("productId") long productId) {
39+
ProductList productList = productListRepository.findOne(listId);
40+
Product product = productRepository.findOne(productId);
41+
42+
productList.getProducts().add(product);
43+
productListRepository.save(productList);
44+
}
2945
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tutorial.api.controllers.advices;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
8+
import javax.servlet.http.HttpServletResponse;
9+
import javax.validation.ConstraintViolation;
10+
import javax.validation.ConstraintViolationException;
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
14+
@ControllerAdvice
15+
public class ExceptionHandlingControllerAdvice {
16+
17+
@ResponseStatus(value= HttpStatus.BAD_REQUEST)
18+
@ExceptionHandler(ConstraintViolationException.class)
19+
public void error(ConstraintViolationException exception,
20+
HttpServletResponse response) throws IOException {
21+
ArrayList<String> messages = new ArrayList<>();
22+
23+
for (ConstraintViolation violation : exception.getConstraintViolations()) {
24+
messages.add(violation.getPropertyPath().toString() + ": " + violation.getMessage());
25+
}
26+
response.sendError(HttpStatus.BAD_REQUEST.value(), String.join(", ", messages));
27+
}
28+
29+
}

src/main/java/tutorial/persistence/models/Product.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.fasterxml.jackson.annotation.JsonBackReference;
44

55
import javax.persistence.*;
6+
import javax.validation.constraints.DecimalMin;
7+
import javax.validation.constraints.NotNull;
8+
import javax.validation.constraints.Size;
69
import java.math.BigDecimal;
710
import java.util.List;
811

@@ -14,9 +17,13 @@ public class Product {
1417
private long id;
1518

1619
@Column(nullable=false)
20+
@NotNull
21+
@Size(min=1)
1722
private String name;
1823

1924
@Column(nullable=false)
25+
@NotNull
26+
@DecimalMin(value="0")
2027
private BigDecimal price;
2128

2229
@ManyToMany(mappedBy="products")

src/main/java/tutorial/persistence/models/ProductList.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonManagedReference;
44

55
import javax.persistence.*;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
68
import java.util.List;
79

810
@Entity
@@ -13,6 +15,8 @@ public class ProductList {
1315
private long id;
1416

1517
@Column(nullable=false)
18+
@NotNull
19+
@Size(min=5)
1620
private String name;
1721

1822
@ManyToMany

0 commit comments

Comments
 (0)