From 0bdc3d6e1e9890c542f8d8797570cdf05d595538 Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Thu, 19 Oct 2023 22:18:21 +0300 Subject: [PATCH] Validation added --- pom.xml | 4 ++ .../bookstore/controller/BookController.java | 5 +- .../bookstore/dto/CreateBookRequestDto.java | 7 +++ .../exceptions/GlobalExceptionHandler.java | 46 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/bookstore/exceptions/GlobalExceptionHandler.java diff --git a/pom.xml b/pom.xml index 518022e..dacbfce 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,10 @@ liquibase-core ${liquibase.version} + + org.hibernate.validator + hibernate-validator + diff --git a/src/main/java/org/bookstore/controller/BookController.java b/src/main/java/org/bookstore/controller/BookController.java index 5d76e9c..d6ded4a 100644 --- a/src/main/java/org/bookstore/controller/BookController.java +++ b/src/main/java/org/bookstore/controller/BookController.java @@ -1,5 +1,6 @@ package org.bookstore.controller; +import jakarta.validation.Valid; import java.util.List; import lombok.RequiredArgsConstructor; import org.bookstore.dto.BookDto; @@ -31,13 +32,13 @@ public BookDto getBookById(@PathVariable Long id) { } @PostMapping - public BookDto createBook(@RequestBody CreateBookRequestDto bookRequestDto) { + public BookDto createBook(@RequestBody @Valid CreateBookRequestDto bookRequestDto) { return bookService.save(bookRequestDto); } @PutMapping("/{id}") public void updateBookById(@PathVariable Long id, - @RequestBody CreateBookRequestDto bookRequestDto) { + @RequestBody @Valid CreateBookRequestDto bookRequestDto) { bookService.updateBookById(id, bookRequestDto); } diff --git a/src/main/java/org/bookstore/dto/CreateBookRequestDto.java b/src/main/java/org/bookstore/dto/CreateBookRequestDto.java index 70977a2..c7006ef 100644 --- a/src/main/java/org/bookstore/dto/CreateBookRequestDto.java +++ b/src/main/java/org/bookstore/dto/CreateBookRequestDto.java @@ -1,14 +1,21 @@ package org.bookstore.dto; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; import java.math.BigDecimal; import lombok.Data; @Data public class CreateBookRequestDto { private Long id; + @NotNull private String title; + @NotNull private String author; + @NotNull private String isbn; + @NotNull + @Min(0) private BigDecimal price; private String description; private String coverImage; diff --git a/src/main/java/org/bookstore/exceptions/GlobalExceptionHandler.java b/src/main/java/org/bookstore/exceptions/GlobalExceptionHandler.java new file mode 100644 index 0000000..803d5db --- /dev/null +++ b/src/main/java/org/bookstore/exceptions/GlobalExceptionHandler.java @@ -0,0 +1,46 @@ +package org.bookstore.exceptions; + +import java.time.LocalDateTime; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { + @Override + protected ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, + HttpHeaders headers, + HttpStatusCode status, + WebRequest request) { + Map body = new LinkedHashMap<>(); + body.put("time", LocalDateTime.now()); + body.put("status", HttpStatus.BAD_REQUEST); + List error = ex.getBindingResult().getAllErrors() + .stream() + .map(this::getErrorMessage) + .collect(Collectors.toList()); + body.put("errors", error); + return new ResponseEntity<>(body, headers, status); + } + + private String getErrorMessage(ObjectError objectError) { + if (objectError instanceof FieldError) { + String field = ((FieldError) objectError).getField(); + String message = objectError.getDefaultMessage(); + return field + " " + message; + } + return objectError.getDefaultMessage(); + } +}