From: Kai Moritz Date: Sun, 13 Jun 2021 08:39:04 +0000 (+0200) Subject: WIP X-Git-Tag: wip-initialer-commit~3 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=aa542b5bd81fe02b9610c9348f327655fb1de0fc;p=demos%2Fkafka%2Fdemos-kafka-payment-system-setup WIP --- diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java index b39322a..ff9efc0 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferController.java @@ -5,12 +5,20 @@ import de.juplo.kafka.payment.transfer.domain.Transfer; import de.juplo.kafka.payment.transfer.domain.TransferService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import java.net.URI; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; @RestController @@ -54,4 +62,28 @@ import java.net.URI; .map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer))) .orElse(ResponseEntity.notFound().build()); } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + public Map handleValidationExceptions( + HttpServletRequest request, + MethodArgumentNotValidException e) + { + Map errorAttributes = new HashMap<>(); + errorAttributes.put("status", HttpStatus.BAD_REQUEST.value()); + errorAttributes.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase()); + errorAttributes.put("path", request.getRequestURI()); + errorAttributes.put("method", request.getMethod()); + errorAttributes.put("request", request.get) + errorAttributes.put("timestamp", new Date()); + Map errors = new HashMap<>(); + e.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + errorAttributes.put("errors", errors); + errorAttributes.put("message", "Validation failed: Invalid message format, error count: " + errors.size()); + return errorAttributes; + } } diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java index 1d2ffa8..ad4f57d 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/controller/TransferDTO.java @@ -19,13 +19,13 @@ public class TransferDTO @Min(value = 1, message = "A valid transfer id must be a positive number") private Long id; @NotNull(message = "Cannot be null") - @Min(value = 1, message = "A valid banc account id must be a positive number") + @Min(value = 1, message = "A valid bank account id must be a positive number") private Long payer; @NotNull(message = "Cannot be null") - @Min(value = 1, message = "A valid banc account id must be a positive number") + @Min(value = 1, message = "A valid bank account id must be a positive number") private Long payee; @NotNull(message = "Cannot be null") - @Min(value = 1, message = "Cannot transfer a non-positiv amount") + @Min(value = 1, message = "The amount of a transfer must be a positv value") private Integer amount; private Transfer.State state;