From 6ee6872892a70513d4f821f7a10be1a5c06b31e2 Mon Sep 17 00:00:00 2001 From: uPagge Date: Mon, 22 Mar 2021 19:37:14 +0300 Subject: [PATCH] ControllerAdvice --- .../ErrorHandlingControllerAdvice.java | 48 +++++++++++++++++++ .../springvalidation/dto/PersonDto.java | 5 +- .../dto/ValidationErrorResponse.java | 14 ++++++ .../springvalidation/dto/Violation.java | 13 +++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/sadtech/example/springvalidation/controller/ErrorHandlingControllerAdvice.java create mode 100644 src/main/java/org/sadtech/example/springvalidation/dto/ValidationErrorResponse.java create mode 100644 src/main/java/org/sadtech/example/springvalidation/dto/Violation.java diff --git a/src/main/java/org/sadtech/example/springvalidation/controller/ErrorHandlingControllerAdvice.java b/src/main/java/org/sadtech/example/springvalidation/controller/ErrorHandlingControllerAdvice.java new file mode 100644 index 0000000..6961524 --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/controller/ErrorHandlingControllerAdvice.java @@ -0,0 +1,48 @@ +package org.sadtech.example.springvalidation.controller; + +import org.sadtech.example.springvalidation.dto.ValidationErrorResponse; +import org.sadtech.example.springvalidation.dto.Violation; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import javax.validation.ConstraintViolationException; +import java.util.List; +import java.util.stream.Collectors; + +@ControllerAdvice +public class ErrorHandlingControllerAdvice { + + @ResponseBody + @ExceptionHandler(ConstraintViolationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ValidationErrorResponse onConstraintValidationException( + ConstraintViolationException e + ) { + final List violations = e.getConstraintViolations().stream() + .map( + violation -> new Violation( + violation.getPropertyPath().toString(), + violation.getMessage() + ) + ) + .collect(Collectors.toList()); + return new ValidationErrorResponse(violations); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ResponseBody + public ValidationErrorResponse onMethodArgumentNotValidException( + MethodArgumentNotValidException e + ) { + final List violations = e.getBindingResult().getFieldErrors().stream() + .map(error -> new Violation(error.getField(), error.getDefaultMessage())) + .collect(Collectors.toList()); + return new ValidationErrorResponse(violations); + } + +} diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java index 4a311fe..09f29e0 100644 --- a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java +++ b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java @@ -34,7 +34,10 @@ public class PersonDto { private int numberBetweenOneAndTen; @Column(name = "ip_address") - @Pattern(regexp = "^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$") + @Pattern( + regexp = "^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$", + message = "Не соответствует формату IP адреса" + ) private String ipAddress; } diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/ValidationErrorResponse.java b/src/main/java/org/sadtech/example/springvalidation/dto/ValidationErrorResponse.java new file mode 100644 index 0000000..1604b55 --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/dto/ValidationErrorResponse.java @@ -0,0 +1,14 @@ +package org.sadtech.example.springvalidation.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +@Getter +@RequiredArgsConstructor +public class ValidationErrorResponse { + + private final List violations; + +} diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/Violation.java b/src/main/java/org/sadtech/example/springvalidation/dto/Violation.java new file mode 100644 index 0000000..c8e79a8 --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/dto/Violation.java @@ -0,0 +1,13 @@ +package org.sadtech.example.springvalidation.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class Violation { + + private final String fieldName; + private final String message; + +}