Я могу передавать значения с плавающей запятой любого типа от 1,0 до 99,99. Однако когда я прохожу через 99A.99, он возвращает ошибку 400 Bad Request
Код: Выделить всё
JSON parse error: Cannot deserialize value of type `java.lang.Float` from String \"99A.99\": not a valid `Float` value
Код: Выделить всё
public class BookDTO {
@NotNull
@DecimalMin(value = "0.00", inclusive = true, message = "Price should not be less than 0.00")
@DecimalMax(value = "99.99", inclusive = true, message = "Price should not be more than 99.99")
private Float price;
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
Ниже я попробовал:
ValidFloatFormat
Код: Выделить всё
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FloatFormatValidator.class)
public @interface ValidFloatFormat {
String message() default "Invalid float format";
Class[] groups() default {};
Class createBook(@Valid @RequestBody BookDTO bookDTO, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
ValidationErrorResponse validationErrorResponse = new ValidationErrorResponse();
validationErrorResponse.setStatus(HttpStatus.BAD_REQUEST.value());
validationErrorResponse.setError(HttpStatus.BAD_REQUEST.name());
for (FieldError fieldError : bindingResult.getFieldErrors()) {
validationErrorResponse.addError(fieldError.getField(), fieldError.getDefaultMessage());
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(validationErrorResponse);
}
BookDTO createdBook = bookService.createBook(bookDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(createdBook);
}
Код: Выделить всё
public class ValidationErrorResponse {
private Map errors = new HashMap();
public Map getErrors() {
return errors;
}
... // other columns
public void addError(String field, String errorMessage) {
errors.computeIfAbsent(field, k -> new ArrayList()).add(errorMessage);
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-type-dto