Skip to content

Commit

Permalink
Added Bean Validation. Closes springframeworkguru#8
Browse files Browse the repository at this point in the history
  • Loading branch information
IusanMihai committed Aug 24, 2022
1 parent 89aee57 commit 2b597a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -26,7 +27,7 @@ public ResponseEntity<BeerDto> getBeer(@PathVariable("beerId") UUID beerId){
}

@PostMapping // POST - create new beer
public ResponseEntity<HttpHeaders> handelPost(@RequestBody BeerDto beerDto){
public ResponseEntity<HttpHeaders> handelPost(@Valid @RequestBody BeerDto beerDto){
BeerDto savedDto = beerService.saveNewBeer(beerDto);

HttpHeaders headers = new HttpHeaders();
Expand All @@ -37,7 +38,7 @@ public ResponseEntity<HttpHeaders> handelPost(@RequestBody BeerDto beerDto){
}

@PutMapping({"/{beerId}"})
public ResponseEntity<HttpStatus> handleUpdate(@PathVariable("beerId") UUID beerId, @RequestBody BeerDto beerDto){
public ResponseEntity<HttpStatus> handleUpdate(@PathVariable("beerId") UUID beerId, @Valid @RequestBody BeerDto beerDto){
beerService.updateBeer(beerId, beerDto);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,8 +16,15 @@
@Builder
public class BeerDto {

@Null
private UUID id;

@NotBlank
private String beerName;

@NotBlank
private String beerStyle;

@Positive
private Long upc;
}

0 comments on commit 2b597a7

Please sign in to comment.