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
.map(transfer -> ResponseEntity.ok(TransferDTO.of(transfer)))
.orElse(ResponseEntity.notFound().build());
}
+
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
+ @ExceptionHandler(MethodArgumentNotValidException.class)
+ public Map<String, Object> handleValidationExceptions(
+ HttpServletRequest request,
+ MethodArgumentNotValidException e)
+ {
+ Map<String, Object> 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<String, String> 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;
+ }
}
@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;