From 2b597a78b12259fe78ad4f2eff95b20132189191 Mon Sep 17 00:00:00 2001 From: mihai Date: Wed, 24 Aug 2022 15:43:07 +0200 Subject: [PATCH] Added Bean Validation. Closes #8 --- pom.xml | 4 ++++ .../msscbrewery/web/controller/BeerController.java | 5 +++-- .../springframework/msscbrewery/web/model/BeerDto.java | 10 ++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 293920ee..f76d4c01 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot diff --git a/src/main/java/guru/springframework/msscbrewery/web/controller/BeerController.java b/src/main/java/guru/springframework/msscbrewery/web/controller/BeerController.java index 532b40b4..0150d459 100644 --- a/src/main/java/guru/springframework/msscbrewery/web/controller/BeerController.java +++ b/src/main/java/guru/springframework/msscbrewery/web/controller/BeerController.java @@ -7,6 +7,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; import java.util.UUID; @RequestMapping("/api/v1/beer") @@ -26,7 +27,7 @@ public ResponseEntity getBeer(@PathVariable("beerId") UUID beerId){ } @PostMapping // POST - create new beer - public ResponseEntity handelPost(@RequestBody BeerDto beerDto){ + public ResponseEntity handelPost(@Valid @RequestBody BeerDto beerDto){ BeerDto savedDto = beerService.saveNewBeer(beerDto); HttpHeaders headers = new HttpHeaders(); @@ -37,7 +38,7 @@ public ResponseEntity handelPost(@RequestBody BeerDto beerDto){ } @PutMapping({"/{beerId}"}) - public ResponseEntity handleUpdate(@PathVariable("beerId") UUID beerId, @RequestBody BeerDto beerDto){ + public ResponseEntity handleUpdate(@PathVariable("beerId") UUID beerId, @Valid @RequestBody BeerDto beerDto){ beerService.updateBeer(beerId, beerDto); return new ResponseEntity<>(HttpStatus.NO_CONTENT); diff --git a/src/main/java/guru/springframework/msscbrewery/web/model/BeerDto.java b/src/main/java/guru/springframework/msscbrewery/web/model/BeerDto.java index d2a26486..76210eba 100644 --- a/src/main/java/guru/springframework/msscbrewery/web/model/BeerDto.java +++ b/src/main/java/guru/springframework/msscbrewery/web/model/BeerDto.java @@ -5,6 +5,9 @@ import lombok.Data; import lombok.NoArgsConstructor; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Null; +import javax.validation.constraints.Positive; import java.util.UUID; @Data @@ -13,8 +16,15 @@ @Builder public class BeerDto { + @Null private UUID id; + + @NotBlank private String beerName; + + @NotBlank private String beerStyle; + + @Positive private Long upc; }