From: Kai Moritz Date: Sun, 13 Jun 2021 08:07:08 +0000 (+0200) Subject: WIP X-Git-Tag: wip-initialer-commit~5 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=5c714e781fff93d8b6975bbbbe6ba2359831f2eb;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 170d8c8..f7f9ff5 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,17 @@ 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.validation.Valid; import java.net.URI; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; @@ -55,4 +60,17 @@ import java.util.Optional; .map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer))) .orElse(ResponseEntity.notFound().build()); } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + public Map handleValidationExceptions( + MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + return errors; + } } 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 6f1310e..1d2ffa8 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 @@ -4,6 +4,7 @@ import de.juplo.kafka.payment.transfer.domain.Transfer; import lombok.Builder; import lombok.Data; +import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; @@ -15,13 +16,17 @@ import javax.validation.constraints.NotNull; public class TransferDTO { @NotNull(message = "Cannot be null") - private long id; + @Min(value = 1, message = "A valid transfer id must be a positive number") + private Long id; @NotNull(message = "Cannot be null") - private long payer; + @Min(value = 1, message = "A valid banc account id must be a positive number") + private Long payer; @NotNull(message = "Cannot be null") - private long payee; + @Min(value = 1, message = "A valid banc account id must be a positive number") + private Long payee; @NotNull(message = "Cannot be null") - private int amount; + @Min(value = 1, message = "Cannot transfer a non-positiv amount") + private Integer amount; private Transfer.State state;